[PyKDE] Inserting widgets "into" a QGroupBox

David Boddie david at boddie.org.uk
Wed Jan 31 01:08:11 GMT 2007


On Tue Jan 30 22:34:46 MET 2007, Tony Cappellini wrote:

> I have two QVBoxLayouts on a QDialogue. I want to put the widgets on the
> left side of the window into one group box, and the widgets on the right
> into another group box.

So, I could draw a tree structure like this:

QDialog
  QHBoxLayout
    QVBoxLayout (on the left)
      QGroupBox
    QVBoxLayout (on the right)
      QGroupBox

Would that sort of represent what you're trying to do? If each QVBoxLayout
is only going to contain a single QGroupBox, you don't need to bother with
them - just put the group boxes directly in the QHBoxLayout. In pseudocode:

# self is the dialog in this code
hboxLayout = QHBoxLayout()
hboxLayout.addWidget(leftGroupBox)
hboxLayout.addWidget(rightGroupBox)
self.setLayout(hboxLayout)

If you really do want to put two vertical layouts into a horizontal layout
and put a group box into each of them, you need to do something like this:

# self is the dialog in this code
leftLayout = QVBoxLayout()
leftLayout.addWidget(leftGroupBox)
# ... add other widgets on the left ...
rightLayout = QHBoxLayout()
rightLayout.addWidget(rightGroupBox)
# ... add other widgets on the right ...

hboxLayout = QHBoxLayout()
hboxLayout.addLayout(leftLayout)
hboxLayout.addLayout(rightLayout)
self.setLayout(hboxLayout)

So, if you don't really need those extra layouts, it's a bit more code to
do the same thing.

> I've tried to insert some widgets "into" a QGroupBox, but the widgets
> actually appeared under the group box, not inside it.
> How do I get the widgets to appear "inside" of the group box?

The group box is just an ordinary widget that can be used to contain other
widgets. It needs a layout inside it to organise those other widgets.

> When I called the setLayout() method of QGroupBox and passed it the parent
> layout, the whole window never appears.

That's because the setLayout() method of QGroupBox sets the layout inside
the group box. If you put the layout that's holding the group box itself
inside the group box, I'm not sure what happens!

You need to create a new layout for the child widgets of the group box, add
those widgets to it, and set that layout on the group box.

You can experiment with this sort of thing with Qt Designer. Create a form
that does what you want, save a .ui file, then run it through pyuic4 to see
what kind of code it generates. There will be a fair amount of "boilerplate"
code, but you should be able to see what it's doing with layouts.

Also, you might want to take a look at this presentation and its accompanying
examples for some layout examples, though I notice that it doesn't include
any examples of nested layouts:

http://indico.cern.ch/contributionDisplay.py?contribId=33&sessionId=41&confId=44

David




More information about the PyQt mailing list