41

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?

Alex Ivasyuv
  • 8,585
  • 17
  • 72
  • 90

7 Answers7

45

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.

John_West
  • 2,239
  • 4
  • 24
  • 44
Frank Osterfeld
  • 24,815
  • 5
  • 58
  • 70
  • 11
    The key point is that a layout can become a child of a widget (because they both inherit `QObject`), but a widget cannot become a child of a layout. A widget must have another widget as a parent, and `QLayout` does not inherit `QWidget`. Layouts wrap each item they contain in a [`QLayoutItem`](http://doc.qt.io/qt-4.8/qlayoutitem.html#QLayoutItem), and so a different set of APIs is required to access the underlying object. – ekhumoro May 13 '15 at 16:26
  • Sucks that this comment is four years old. The way Qt even today makes QLayout keep track of the widgets inside it, is the private d_ptr and its list property. No public API... – AIDoubt Dec 17 '19 at 13:20
43

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()
  }
}
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
braggPeaks
  • 1,158
  • 10
  • 23
11

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

for (int i = 0; i < this->layout->count(); ++i) {
    QWidget *w = this->layout->itemAt(i)->widget();
    if(w != NULL)
        w->setVisible(false);
}
isamert
  • 482
  • 4
  • 12
  • Must be implemented in subclasses to return the layout item at index. If there is no such item, the function must return 0. that's quoted from https://doc.qt.io/qt-5/qlayout.html#itemAt – stdcerr Aug 04 '19 at 17:57
2

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

  QLayout * top_l= layout(); // The parent widgets layout
   // Find your layout that you want to search inside
   QHBoxLayout * hbox = top_l->findChild<QHBoxLayout*>(QString("horizontalLayout_2"));
    if (hbox != 0) {
        std::cout << "Found horizontalLayout_2!"<<std::endl;
        QPushButton * st = findChild<QPushButton*>(QString("startButton"));

        if (st != 0) {
            std::cout << "Found startButton in top level widget"<<std::endl;
            int idx = hbox->indexOf(st);
            if (idx >=0) {
                std::cout << "Found startButton in hbox layout at location : "
                          <<idx<<std::endl;
            }
        }
    };
JCS
  • 21
  • 1
1

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:

void setEnabledWidgetsInLayout(QLayout *layout, bool enabled)
{
   if (layout == NULL)
      return;

   QWidget *pw = layout->parentWidget();
   if (pw == NULL)
      return;

   foreach(QWidget *w, pw->findChildren<QWidget*>())
   {
      if (isChildWidgetOfAnyLayout(layout,w))
         w->setEnabled(enabled);
   }
}

bool isChildWidgetOfAnyLayout(QLayout *layout, QWidget *widget)
{
   if (layout == NULL or widget == NULL)
      return false;

   if (layout->indexOf(widget) >= 0)
      return true;

   foreach(QObject *o, layout->children())
   {
      if (isChildWidgetOfAnyLayout((QLayout*)o,widget))
         return true;
   }

   return false;
}
supaflav
  • 99
  • 4
1

I think it is easier to create a QWidget as a container and put your widgets inside of that "container widget", this way you could access your widgets by calling findChildren on the "container widget":

auto children = ui->containerWidget->findChildren<QWidget *>();
for (auto child : children) {
    child->setVisible(false);
}
Hani Shams
  • 331
  • 3
  • 5
  • In my case, there is already a groupbox as a parent of the horizontal layout. So changing ui->layout->findChildren to ui->groupBox->findChildren has solved. – atakli Aug 17 '22 at 13:16
-1

Did you try children() method instead of findChildren() ? Maybe you are getting a 'bad' layout from widget -> 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.

Andrew
  • 24,218
  • 13
  • 61
  • 90
  • Yes I tried children(), without any luck. I can't check after creation, as this is loaded from .ui... For widgets works fine... Issue only with layout.. – Alex Ivasyuv Oct 31 '10 at 23:27