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?
You can simply iterate over the layout's items, using
itemAt()
, then test whether the item is a widget:Responding to an old post, but I wanted a simple way to disable all widgets contained in a layout or any child-layout. This worked for my purposes:
It's very late but if anyone finds here like me, here is my solution: I tried @braggPeaks answer(it's same as @Frank Osterfeld answer) but it failed. Then I modified like this and it works like a charm. (I have no idea why it works, because my layout has no null items but still I have to check if it has.)
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()
andQLayout::itemAt()
instead.Did you try
children()
method instead offindChildren()
? Maybe you are getting a 'bad' layout fromwidget -> findChild<QLayout *> (layoutName)
method. Try to find children right after creating the layout - so you are sure the layout is correct. Doing so you will be able do determine what function works wrong.Since layout is not part of widget hierarchy, the widget has to be queried from parent but then indexOf can be used to see if it belongs and its location