Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt get children from layout

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?

like image 989
Alex Ivasyuv Avatar asked Oct 31 '10 22:10

Alex Ivasyuv


People also ask

What is stretch factor Qt?

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.

What is the correct method to add child layout is?

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.

What is Qtwidgets?

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.

What is QVBoxLayout?

The QVBoxLayout class lines up widgets vertically. This class is used to construct vertical box layout objects. See QBoxLayout for details.


2 Answers

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.

like image 108
Frank Osterfeld Avatar answered Sep 19 '22 12:09

Frank Osterfeld


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()   } } 
like image 25
braggPeaks Avatar answered Sep 20 '22 12:09

braggPeaks