9

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?

Julia Meshcheryakova
  • 3,162
  • 3
  • 22
  • 42
Sven
  • 985
  • 1
  • 11
  • 27

2 Answers2

12

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.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • Thanks works nicely! Re size issues, setting minimum sizes in Qt Designer helped. I thought perhaps there is a programmatic way to ensure the main window is sized so that all widgets inside it are visible. – Sven Jan 06 '12 at 00:11
  • Re QUiLoader, no specific reason, other than that it is simple. For the `pyuic4` approach, I'll have to research how to make Eclipse/PyDev start `pyuic4` automatically for .ui files. – Sven Jan 06 '12 at 00:20
  • @Sven. See my updated answer on resizing: I'm pretty sure it is a layout problem. – ekhumoro Jan 06 '12 at 00:29
  • Thanks again, this worked for my resizing troubles. I am beginning to like pyQt! – Sven Jan 13 '12 at 17:30
0

I edited the .ui file:

  1. Close QT designer
  2. Edit the .ui in the slot/signal area
  3. Run it
Julia Meshcheryakova
  • 3,162
  • 3
  • 22
  • 42
Rudi Lueg
  • 1
  • 1