0

Is there a way to bring the already opened QGIS plugin dialog that I wrote to the front with a click on the icon in the QGIS toolbar?

By default, it only opens on click, and to bring it to the front I need to minimize everything in front of it to reaccess the dialog. I haven't made any changes to the default code that the Plugin Builder produced, I only added functionalities that I needed.

1 Answer 1

1

You need to use QWidget method activateWindow() as mentioned in this answer by Avaris.

You can then modify your plugin from this :

    def run(self):
        """Run method that performs all the real work"""

        # Create the dialog with elements (after translation) and keep reference
        # Only create GUI ONCE in callback, so that it will only load when the plugin is started
        if self.first_start == True:
            self.first_start = False
            self.dlg = YourPluginDialog()
        # show the dialog
        self.dlg.show()
        # Run the dialog event loop
        result = self.dlg.exec_()
        # See if OK was pressed
        if result:
            # Do something

To this :

    def run(self):
        """Run method that performs all the real work"""

        # Create the dialog with elements (after translation) and keep reference
        # Only create GUI ONCE in callback, so that it will only load when the plugin is started
        if self.first_start == True:
            self.first_start = False
            self.dlg = YourPluginDialog()
            # show the dialog
            self.dlg.show()
        else:
            self.dlg.activateWindow()
        # Run the dialog event loop
        result = self.dlg.exec_()
        print(result)
        # See if OK was pressed
        if result == 1:
            # Do something

result must be equal to 1 because reopening the dialog might give result the value of -1, so if result is valid. 1 is the outcome when the user is pressing the OK button.

1
  • Thank you so much! It works exactly as I needed it to! Jun 5, 2023 at 20:41

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.