pythonpyqtpysidesignals-slotsqt-designer

How to implement a signal/slot defined in Qt Designer


I am trying to connect the click() signal of a button to my own function. The button is in a widget that I created with QT Designer. I load the .ui file with QUiLoader like so:

class MyWidget(QtGui.QMainWindow):
    def __init__(self, *args):  
        QtGui.QMainWindow.__init__(self, *args)

        loader = QtUiTools.QUiLoader()
        file = QtCore.QFile("pyside_ui_qtdesigner_form_test.ui")
        file.open(QtCore.QFile.ReadOnly)
        self.myWidget = loader.load(file, self)
        file.close()

        self.setCentralWidget(self.myWidget)

        btn = self.myWidget.findChild(QtGui.QPushButton, "HelloWorldButton")
        btn.clicked.connect(self.slot1)        
    
    def slot1(self):
        print "Received"

Is this the correct way to connect to button clicked() signal? I see that I can wire up signals and slots directly in Qt Designer, but how do I prepare and get to such wire-ups in the code? Side question: The code above works, but the main window shows in the wrong size. How do I ensure that it appears in the right size? Should I do this with minimum height/width constraints?


Solution

  • Use Signals and Slots Editing Mode for connecting predefined Qt signals directly to predefined Qt slots.

    So for "Close" button on a simple dialog, you can just drag a connection from the button to the dialog, select the clicked() signal and the reject() slot, click "OK", and there would be nothing more to do.

    For signals and/or slots you want to define yourself, you can either make the connections directly in code, or indirectly via Qt Designer. Your example already demonstrates the first option fairly well, but the connection could be done much more simply and cleanly, like this:

    self.myWidget.HelloWorldButton.clicked.connect(self.slot1)
    

    Making the connection in Qt Designer is more complicated. Firstly, you must switch to Signal/Slot editing mode (by pressing F4 or clicking the toolbar button) and then drag a connection between the sender/receiver objects. This will open the Configure Connection dialog where you can click one of the Edit buttons to open the Signal/Slot Editor, and then click the plus buttons to manually add the signatures of any signals/slots you will define in your own code (see the screenshot below). Once this is done, you can close the second dialog and make the relevant connection(s) in the first dialog. But to be clear: this will not create any signals and slots within Qt Designer itself - all it does is allow you to specify names your code can use later. Also, it's only possible to manually add signals/slots to custom classes, such as the top-level form and promoted widgets.

    screenshot


    As for your main window having the "wrong size": it's difficult to tell from the code you've shown, but it may be because you did not set a layout in the widget that you are loading.

    BTW: is there a specific reason you're using QUiLoader? Compiling python modules using pyuic4 is much more flexible, and you can learn a lot from the code that is generated.

    EDIT

    For me, setting a layout on the main form cures the resizing problem you are talking about.

    If you don't know how to do that: in Designer, right-click on a blank part of the main form, and then select Layout/Layout in a Grid from the menu (there's also a button on the toolbar for this).

    Once you've done that, resizing the form will automatically stretch it to fit the contained widgets.