3

I usually use QGIS Plugin Builder to create working templates for QGIS plugins.

While a generic Python plugin needs the files below;


__init__.py
metadata.txt
resources.py (translated from resources.rcc via pyrcc4)
myPlugin.py
myPlugin_UI.py (translated from myPlugin_UI.ui via pyuic4)

Plugin Builder also creates the following file;


myPlugin_dialog.py

I can understand and use it but why do we need this file myPlugin_dialog.py?

If I import "myPlugin_UI.py" at the beginning, I can already access and interact with the form objects directly from the main module "myPlugin.py"

Why should I need an additional file? What is the advantage of this structure?

(Please explain in basic word since I am not fully fluent with PyQt)

5

Technically the only files you need for a plugin are:

__init__.py
metadata.txt
plugin.py

All the other files are just extra stuff depending on what you plugin does. If you don't need any resources (which you normally do) then you don't need resources.rc.

On to your question:

myPlugin_dialog.py

Just hold a class that inherits from your generated ui files. This is just normal Python inheritance and means you can extend the generated code with more logic if needed.

from myPlugin_UI import dialogUI


class MyDialog(dialogUI, QDialog):
   def __init__(self, parent):
       ....

   def dosomething(self):
       self.label.hide()


dlg = MyDialog()
dlg.dosomething()

You don't need to have this class however if you need to add extra logic to the widget/dialog then this is the preferred method.

  • 1
    Thank you Nathan. I also found a similar explanation that says; "...The whole point of this class is to abstract the the setup of the UI so we don’t have to deal with GUI setup in our main Python module." – cempro Mar 16 '14 at 11:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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