4

I need to create a dock plugin for QGIS using QT designer and Python. I've created a basic plugin in "plugin builder". Then I've changed the .ui so now I dont have standard QDialog box but a QDockWidget. So now i have "plugin_module_dialog.py" that looks like that:

import os

from PyQt4 import QtGui, uic

FORM_CLASS, _ = uic.loadUiType(os.path.join(
    os.path.dirname(__file__), 'testing_module_dialog_base.ui'))

class testing_classDialog(QtGui.QDockWidget, FORM_CLASS):
    def __init__(self, parent=None):
        """Constructor."""
        super(testing_classDialog, self).__init__(parent)

        self.setupUi(self)

and "plugin_module.py" like that:

class testing_class:

    def __init__(self, iface):

        self.iface = iface

        path = os.path.dirname( os.path.abspath( __file__ ) )
        self.dock = uic.loadUi( os.path.join( path, "plugin_module_dialog_base.ui" ) )
        self.iface.addDockWidget( Qt.RightDockWidgetArea, self.dock )
        ...

Now when i run that plugin i get:

AttributeError: 'plugin_classDialog' object has no attribute 'exec_'

And the plugin window pops up normally. Not docked. I am new to that. Please explain me what should I do to get working dock plugin.

1

I had same problem yesterday. Step 1: Build a GUI creator something like (ui2py) to make a py file out of your ui.

import os
os.system("C:/Python27/Lib/site-packages/PyQt4/pyuic4.bat -x testing_module_dialog_base.ui -o testing_module_dialog_base.py")
print("Done!")

Step 2: Your plugin_module_dialog.py will look like that..

import os
from PyQt4.Core import Ot
from PyQt4 import QtGui, uic

from testing_module_dialog_base.py import Testing_Module_Dialog_Base #or whatever the class is called in it

class testing_classDialog(QtGui.QDialog):
    def __init__(self, iface):
        """Constructor."""
        QtGui.QDialog.__init__(self, None, Qt.WindowStaysOnTopHint)
        self.ui = Testing_Module_Dialog_Base()
        self.setupUi(self)
        self.iface = iface

Install PyQt4 in order to do the first step.

Step3 (optional): I strongly recommend you building a "GUI-Runner". It would look like this..

from PyQt4 import QtGui
import sys

from testing_classDialog.py import testing_classDialog

def main():
    app = QtGui.QApplication(sys.argv)
    gui = testing_classDialog(None)
    gui.show()
    app.exec_()

if __name__ == '__main__':
    main()

Have fun.

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.