QGroupBox with two rows of widgets

Maurizio Berti maurizio.berti at gmail.com
Mon May 3 17:30:52 BST 2021


Il giorno lun 3 mag 2021 alle ore 17:22 Rich Shepard <
rshepard at appl-ecosys.com> ha scritto:

> QGroupBox is a container holding layouts with widgets. I want to group six
> widgets -- in two rows -- into a groupbox. Having each row of widgets
> in a group with the same title was successful, but I'm failing at having
> both rows of widgets in a single group box.
>

I believe you're a bit confused about widgets and layouts.

Widgets are UI elements that *can* have layout managers set on them.
Layout managers are items that *manage* the geometry of the children
widgets, they ensure that those widgets are properly resized and
positioned, and that the widget in which the layout is set has a proper
size in order to show all of its contents.
Qt layouts are based on QLayoutItem, which is an abstract object that
represents an item in a layout, and that item can be a widget, another
layout (nested layouts) or a spacer.

You can set a layout by using widget.setLayout(layout) or by adding the
widget as an argument in the constructor: layout = QVBoxLayout(widget).
But, if you want to add a layout *to another one* (nesting), you need to
use addLayout(), and you cannot use the argument in the constructor as
explained before.

Also, using tb1.layout() is pointless, as it will return tb1 anyway.

To summarize:
- set a layout manager on a widget: widget.setLayout(layout) (or the above
constructor);
- add a widget to a layout: layout.addWidget(widget) (see the docs about
the various classes, as there are differences in behavior and some more
functions are provided);
- add a *layout* to a layout: layout.addLayout(otherLayout);
- get the layout currently set on a widget: widget.layout();
- do *not* try to use layout.addItem, which is intended for more advanced
purposes;

So, in your case:
- change all tb1.layout().addWidget(...) to tb1.addWidget(...);
- you can access the layout set on a widget by using widget.layout(), but
since you're creating it in the same scope, there's no need for that: just
create it with a reference and use that afterwards: tboxLayout =
qtw.QVBoxLayout(tbox);
- add the child layout correctly: tboxLayout..addLayout(tb1);
- set the *main* layout for your widget, otherwise you'll certainly face
some issues:
        mainLayout = qtw.QVBoxLayout(self)
        mainLayout.addWidget(tbox)

Read more on https://doc.qt.io/qt-5/layout.html and
https://doc.qt.io/qt-5/designer-layouts.html

As Tomas suggested, you can do some experiments by creating UI in designer
and analyze the output of pyuic to understand how they work. Be aware,
those files should **NEVER** be modified for production. You can study
them, edit them to see the results and understand their behavior, but then
keep in mind that they should always be left as they are if you decide to
use them in your program. Read more about it on the official docs:
https://www.riverbankcomputing.com/static/Docs/PyQt5/designer.html

Maurizio
-- 
È difficile avere una convinzione precisa quando si parla delle ragioni del
cuore. - "Sostiene Pereira", Antonio Tabucchi
http://www.jidesk.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20210503/9736de6e/attachment.htm>


More information about the PyQt mailing list