Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have created an own QDialog (using QT Designer) with three push buttons. The names are pButton1, pButton2 and pButton3. After saving the file I created the ui file by using:

pyuic4 ui_xy.ui > ui_xy.py

I can see my created GUI in QGIS, but I have no idea how to get a reference to the buttons. When I use QMessagebox it is very easy to get to know wich button was pushed, but using the same way for this GUI doesn´t work.

Using QMessagebox the following code works:

ret = QMessageBox(..., QMessageBox.Ok)
if (ret = QMessageBox.Ok):
   DO SOMETHING
else:
   DO SOMETHING

When I use the standard GUI with the Ok and Cancel button, the following code works:

dlg = xyDialog()
dlg.show
result = dlg.exec_()
if result = 0:
...

How can I obtain the same result using my buttons? Replacing the 0 with pbutton1 (or 2 or 3) doesn´t seem to work.

Do you know a good solution or tutorial?

Thanks!

Best,

share|improve this question
add comment

1 Answer 1

up vote 7 down vote accepted

kopi,

QMessageBox, and similar convenience classes, allow for quick creation of standard dialogs, generally using static methods within their class. Creating custom buttons and Qt widgets requires making the Qt signal/slot connections between them, as per the desired GUI interaction for the purpose.

When using QtDesigner and after the compilation of your form to a ui.py file, you will want to import it into a Python module and instantiate it, so that you can directly reference the form's widgets. With PyQGIS plugins, this is usually done in a module specific to the dialog, though with very simple plugins it can be done inside the plugin's base class.

A very good reference for creating PyQGIS plugins, in addition to the PyQGIS Cookbook, is the QGIS Workshop v1.0.0: for help with your question, specifically the section on integrating a tool's output with the GUI:

01  from PyQt4 import QtCore, QtGui
02  from ui_vector_selectbypoint import Ui_vector_selectbypoint
03  
04  class vector_selectbypointDialog(QtGui.QDialog):
05  
06      def __init__(self):
07          QtGui.QDialog.__init__(self)
08          # Set up the user interface from Designer.
09          self.ui = Ui_vector_selectbypoint()
10          self.ui.setupUi(self)

Line by line:

01 Basic PyQt4 imports.
02 Import of custom form's class from its compiled ui file.
04 Your custom dialog's class, which inherits from QDialog.
06 init method called when your dialog is instantiated.
07 Instantiate parent QDialog class, so your class gains its attributes.
09 Instantiate and assign your QtDesigner form class.
10 Call the form class's setupUi() method, so its widgets are available.

Once you have self.ui referencing your custom layout form, you can access its widgets anywhere in your custom dialog class and set up signal/slot connections or named slots.

Example showing new style signal/slot connection (using some object names from comments) and named slot connection:

from PyQt4 import QtCore, QtGui
from ui_ubsearch import Ui_UBSearchDialog  # don't know name of your form class

class UBSearchDialog(QtGui.QDialog):

    def __init__(self):
        QtGui.QDialog.__init__(self)
        # Set up the user interface from Designer.
        self.ui = Ui_UBSearchDialog()
        self.ui.setupUi(self)

        # make new style signal/slot connection
        self.ui.bgrz.clicked.connect(self.exec_grz)

    # slot for connection
    def exec_grz(self):
        # do something
        pass  # <-- replace with actual code

    # named slot example
    # (doesn't require a previously built connection for form widgets)
    @QtCore.pyqtSlot()
    def on_myOtherFormButton_clicked(self):
        # do something
        pass  # <-- replace with actual code

Your UBSearchDialog class will, in turn, need to be instantiated elsewhere (e.g. imported into your plugin's base class and instantiated in its run() method):

ubs_dlg = UBSearchDialog()
ubs_dlg.exec_()

Also, see http://stackoverflow.com/questions/2462401 for more examples concerning named slots and how new style connections keep things cleaner and more 'Pythonic.'


Concerning your second question, there are a couple of things you'll want to clean up before testing more. First, you have multiple imports for the same modules. Your custom dialog module could look like this instead ...

from PyQt4.QtCore import pyqtSlot
from PyQt4.QtGui import QDialog

import datavisualization
from ui_ubsearch import Ui_ubsearch


class ubsearchDialog(QDialog):

    def __init__(self, iface):
        QDialog.__init__(self)

        # Set up the user interface from Designer.
        self.ui = Ui_ubsearch()
        self.ui.setupUi(self)

        # Save reference to the QGIS interface
        self.iface = iface

    @pyqtSlot()
    def on_bgrz_clicked(self):
        # Get an instance of the class
        d = datavisualization.datavisualization(self.iface)

        # Call the method of the intance
        d.data_visualization()

        # Close UI
        self.close()

Of note here is the import of only those names from the PyQt4 modules that are used in the script (QDialog and pyqtSlot). See working with Python modules, for more info. All other unused module imports were removed. You can use pylint to help with finding those unused modules.

Also, when constructing an instance of the class the iface reference is gathered and assigned so it can be passed to your datavisualization module (fixing the parameter error). This means you have to pass iface in from your original plugin module that calls the construction of the custom dialog, e.g. ubsearchDialog(self.iface). So, iface needs a saved reference in your base plugin module as well, in order to be passed to your dialog. This is usually already set up for you if you use tools like Plugin Builder.

You will want to clean up your datavisualization module's imports in a similar fashion.

import os
import datetime
import time
import psycopg2

from PyQt4.QtGui import QMessageBox
from qgis import core

Lastly, you will want to read up on PEP8, Python style guide. Your pasted code contained a mix of spaces and tabs for indention: only-spaces is generally preferred.

share|improve this answer
    
I have found this tutorial. After running through the steps I get still this error message: Wroclaw\apps\qgis\python\plugins\ubsearch\ui_ubsearch.py", line 46, in setupUi QtCore.QObject.connect(self.bgrz, QtCore.SIGNAL(_fromUtf8("clicked()")), ubsearch.exec_grz) AttributeError: 'ubsearchDialog' object has no attribute 'exec_grz'. exec_grz would be the function which I want to run, after clicking the button 'bgrz'. –  kopi Aug 27 '12 at 18:06
1  
AttributeError: 'ubsearchDialog' object has no attribute 'exec_grz' says to me that your exec_grz() slot doesn't exist yet in your code. Without seeing any code context, I'm guessing you would want self.exec_grz instead of ubsearch.exec_grz, if exec_grz() is a defined method of your custom dialog class (e.g. class vector_selectbypointDialog in example above). I'll add more to the signal/slot connection example above. –  dakcarto Aug 27 '12 at 19:02
    
I have tried all these examples already, but maybe I missunderstood something general. I also tried the solution with "self.exec_grz", but the samer error occurs. The function is in my main class. Do I have to put the function into the dialog class? This wouldn´t make any sense in my opinion. Regarding the 2. example using @pyqtslot. When I use this in my main class there will be no error message, but the buttons do not react when I click on them, where do I have to put this method? –  kopi Aug 27 '12 at 19:51
1  
I've updated the example in the answer. The UBSearchDialog class could be in a separate module file, or as an additional class in your plugin's base class module. How you organize your Python code is up to you. –  dakcarto Aug 27 '12 at 20:59
    
It works, thanks! –  kopi Aug 27 '12 at 21:08
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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