0

I'm writing my first QGIS plugin (QGIS version >= 3.18). I created a GUI with QT Designer and then a basic python code to load plugin when QGIS starts:

file __init__.py:

def classFactory(iface):
    from .myFirstPlugin import MyFirstPlugin
    return MyFirstPlugin(iface)

file myFirstPlugin.py:

import os
from qgis.PyQt import uic
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtWidgets import QAction,QDialog

class MyFirstPlugin(QDialog):

    def __init__(self,iface):
        QDialog.__init__(self)
        self.iface = iface
        ui_path = os.path.join(os.path.dirname(__file__),'myfirstplugin_dialog_base.ui')
        uic.loadUi(ui_path,self)

    def initGui(self):
        self.action = QAction('My First Plugin',self.iface.mainWindow())
        self.action.triggered.connect(self.run)
        icon_path = os.path.join(os.path.dirname(__file__),'myfirstplugin.png')
        self.action.setIcon(QIcon(icon_path))
        self.iface.addToolBarIcon(self.action)
        self.iface.addPluginToMenu('&My First Plugin',self.action)

    def unload(self):
        self.iface.removePluginMenu('&My First Plugin',self.action)
        self.iface.removeToolBarIcon(self.action)

    def run(self):
        self.setModal(True)
        self.show()
        self.initialize()
        result = self.exec_()
        if result == QDialog.Accepted:
            pass

where myfirstplugin_dialog_base.ui and myfirstplugin.png are respectively the GUI created with QT Designer and the icon that appear on the button in the toolbar of QGIS main window. The plugin starts regularly without any problems.

Now, I want to run the plugin and keep QGIS main window responsive. I found a lot of solutions about keeping QGIS plugin GUI responsive while running process, but nothing regarding QGIS main window. From what I've read, the correct way is to start the plugin on a separate thread (for example using QThread), but I just don't know how to do that.

Any suggestions?

1 Answer 1

1

The answer to my question was simpler than I expected. After several attempts to start the plugin on a separate thread, all of which failed, I simply modified the instruction

self.setModal(True)

to

self.setModal(False)

in this way, the main QGIS window remains responsive after the plugin GUI is started.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.