QGroupBox with two rows of widgets

Rich Shepard rshepard at appl-ecosys.com
Mon May 3 18:02:12 BST 2021


On Mon, 3 May 2021, Maurizio Berti wrote:

> 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)

Maurizio,

Still the same error:
$ python biomwe.py 
Traceback (most recent call last):
   File "biomwe.py", line 92, in <module>
     bio = BiotaWindow()
   File "biomwe.py", line 72, in __init__
     tbox.addLayout(tb1)
AttributeError: 'QGroupBox' object has no attribute 'addLayout'

Had no issues adding two 'Taxonomy' groups, one for each row. But that looks
sloppy so I want to put both horizontal row boxes in a vertical row within a
group container. So, ...

> 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

I'll do both of the above.

Regards,

Rich


More information about the PyQt mailing list