How to implement a signal/slot defined in Qt Designer

Question:

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 with the right size? Should I do this with minimum height/width constraints?

Asked By: Sven

||

Answers:

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 do not need to “prepare” anything in Designer beforehand. Everything should be done in your own code.

Your example already demonstrates this fairly well, but the connection could be done much more simply and cleanly, like this:

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

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.

Answered By: ekhumoro

I edited the .ui file:

  1. Close QT designer
  2. Edit the .ui in the slot / signal area
  3. Run it
Answered By: Rudi Lueg