I try to hide all widgets in layout. But looks like findChildren
doesn't work for layout.
Here's my sample code:
QLayout * layout = widget -> findChild<QLayout *> (layoutName); QList<QWidget *> list = layout -> findChildren<QWidget *> (); cout << list.size() << endl;
size
is 0, but inside this layout I have a few widgets. But the same code works fine if I try to get widgets from parent widget.
How I can get them from appropriate layout?
Stretch factors are used to change how much space widgets are given in proportion to one another. If we apply stretch factors to each widget, they will be laid out in proportion (but never less than their minimum size hint), e.g.
the correct method to add child layout is: a setlayout() b addlayout() c addwidget() The Qt layout system provides a simple and powerful way of automatically arranging child widgets within a widget to ensure that they make good use of the available space.
Widgets are the primary elements for creating user interfaces in Qt. Widgets can display data and status information, receive user input, and provide a container for other widgets that should be grouped together. A widget that is not embedded in a parent widget is called a window.
The QVBoxLayout class lines up widgets vertically. This class is used to construct vertical box layout objects. See QBoxLayout for details.
The layout does not "inject" itself in the parent-child tree, so the widgets stay (direct) children of their parent widget.
You could use QLayout::count()
and QLayout::itemAt()
instead.
You can simply iterate over the layout's items, using itemAt()
, then test whether the item is a widget:
for (int i = 0; i < gridLayout->count(); ++i) { QWidget *widget = gridLayout->itemAt(i)->widget(); if (widget != NULL) { widget->setVisible(false); } else { // You may want to recurse, or perform different actions on layouts. // See gridLayout->itemAt(i)->layout() } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With