[PyQt] Implementing Qt-Designer Widgets in Main Window and lay them out

David Boddie dboddie at trolltech.com
Wed Jul 15 16:25:07 BST 2009


On Wed Jul 15 15:46:30 BST 2009, Michael Adolph wrote:

> I've got a problem with Qt-Designer and Python. It's pretty basic but I
> can't find any solution.
>
> I designed to forms in Qt-Designer and I'm writing a QMainWindow class in
> Python. The widgets are imported and should be layouted. My Problem ist,
> that both widgets are shown one over another, not caring about my layout.
> This reduces usability pretty much.

The solution is quite simple. You need to call the setupUi() method of each
generated widget and pass the widget itself (not the parent).

Here's your code with my changes:

  class BasisTest(QtGui.QMainWindow):
      def __init__(self):
          QtGui.QMainWindow.__init__(self)
          self.centralWidget=QtGui.QWidget(self)
          self.setCentralWidget(self.centralWidget)
          self.label=QtGui.QLabel("Test",self.centralWidget) # two Labels for
                                                             # Testing
          self.label2=QtGui.QLabel("schalalala",self.centralWidget)
          self.Uebersicht=QtGui.QWidget() # instanciate first Ui-Widget
          ui = Uebersicht.Ui_Uebersicht()
          ui.setupUi(self.Uebersicht) #show Widget
          
          self.Dialog=QtGui.QWidget()
          ui = liegenschaftAuswahlDlg.Ui_liegenschaftAuswahlDlg()
          ui.setupUi(self.Dialog)

> Any hints how to layout imported Widgets? Code is appended. I had to do
> some minor changes by hand in the Qt-Designer generated code (or I'm just
> to much of a newbee...), so I added comments, wherever I tried to be
> smarter then Qt-Designer

The classes generated by pyuic4 are used to add objects to existing widgets;
they aren't actually widgets in their own right. This system makes it
possible to set up blank widgets (as above) or to subclass from a widget
and "mix in" the generated class.

If you use the classes in the way shown above, you shouldn't need to edit the
generated code.

David


More information about the PyQt mailing list