[GIS] Close dialog when algorithm is finished (QGIS plugin)

pyqgispyqtqgis-plugins

I've read many threads on this but I'm not able to figure out the solution (I'm a beginner to the plugin creation…).

What I what to do is to close the dialog window when the algorithm has finished its job.

That's the piece of code of the alg.dialog.py:

    class CreateModelDialog(QDialog, FORM_CLASS):
        def __init__(self, iface):
            QDialog.__init__(self)
            self.iface = iface
            self.setupUi(self)
            self.buttonBox.rejected.connect(self.reject)
            self.buttonBox.button(QDialogButtonBox.Ok).clicked.connect(self.createModel)

            self.manageGui()

........

    def createModel(self):
        modelName = self.bxDBname.text()
        modelType = self.cmbBxModelType.currentText()
        lengthString = self.cmbBxLengthUnit.currentText()
        timeString = self.cmbBxTimeUnit.currentText()
        workingDir = self.OutFilePath
        print 'Direttori recuperata ..... ' , workingDir
        isChild = 1.0
        createModel(modelName, workingDir, modelType, isChild, lengthString, timeString)

So self.buttonBox.button(QDialogButtonBox.Ok).clicked.connect(self.createModel), when clicked runs the createModel method.

But the dialog remains opended, where should I edit the code?


This is the __init__ function:

def __init__(self, iface):
    QDialog.__init__(self)
    self.iface = iface
    self.setupUi(self)
    self.buttonBox.rejected.connect(self.reject)
    self.buttonBox.button(QDialogButtonBox.Ok).clicked.connect(self.createModel)

    self.manageGui()

This is the function in the main plugin.py file:

def runCreateModel(self):
    """Run method that performs all the real work"""
    # Create the dialog (after translation) and keep reference
    self.dlgMD = mdDialog.CreateModelDialog(self.iface)
    # show the dialog
    self.dlgMD.show()
    # Run the dialog event loop
    self.dlgMD.exec_()

Best Answer

In your __init__ function in plugin implementation find a line where your dialog was created.

Probably it will be:

self.dlg = CreateModelDialog()

Then if you want to close dialog window you have to execute:

self.dlg.close()

Put this line into the function with your algorithm at the end of code.

Example structure of your plugin.py:

class CreateModel:
    def __init__(self, iface):
        ...
        self.dlg = CreateModelDialog()
        ...
        # connections
        self.dlg.button.clicked.connect(self.function)

    def function(self):
        # some code for your algorithm
        self.dlg.close()

    ...
    def run(self):
        ...
Related Question