1

I'm trying to open a modeler dialog from a Python plugin using a given .model3 file.

The problem is that the dialog closes immediately even though it seems to successfully load the model file.

I used the post below as a sample, which is pretty much the same but through the Python console:

Running the Graphic Modeler from the Python Console in QGIS

Using the console does work, but through a connect (click) event from a plugin, the window closes immediately.

The dialog code:

    dlg = ModelerDialog()
    dlg.loadModel('C:/BGTGenerator.model3')
    dlg.show()

How can I prevent the modeler dialog from closing or open it correctly from a plugin?

2
  • what version of QGIS are you running this in? I just ran the same code on a local model that I have saved, and dialog opens and stays open. Im running QGIS 3.18. So its either going to be your QGIS version, or possibly something with that model. try also running it using a different model. (like a blank one!)
    – nr_aus
    Commented Feb 2, 2022 at 0:52
  • In a plugin you should declare the dialog object e.g self.mod_dlg = ModelerDialog() in the __init__() method of your main plugin class. Then in your slot method do self.mod_dlg.loadModel('C:/BGTGenerator.model3'); self.mod_dlg.show()
    – Ben W
    Commented Feb 2, 2022 at 1:26

1 Answer 1

1

Fixed it. Changed the code to:

self.dlg = ModelerDialog()
dlg.loadModel('C:/BGTGenerator.model3')
dlg.show()

With the help of Ben W 's comment.

2
  • After declaring self.dlg as a class attribute, all references thereafter should also be self.dlg. Glad you solved your problem, though.
    – Ben W
    Commented Feb 2, 2022 at 10:07
  • Additionally, as I said in my comment, I strongly recommend putting self.dlg = ModelerDialog() inside the __init__() method of the plugin class so that the instance of the dialog class is only created once when the plugin is loaded, not every time the slot method is called.
    – Ben W
    Commented Feb 2, 2022 at 13:33

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.