2

I have a QVBoxLayout created through the drag and drop section. Inside of it AT RUN TIME I insert some widgets with the command

ui->verticalLayout->insertWidget() //using appropriate options.

All widgets I insert are of the same type/class.

I would like to cycle through the inserted widgets in order to perform some actions over them.

I suppose it is really simple but can't seem to find out how...

thank you all!

Wing
  • 642
  • 2
  • 5
  • 16

1 Answers1

2

You can use QLayout::itemAt() to loop on the items of the layout. Then use QLayoutItem::widget() to get the widget:

for(int i = 0; i < layout->count(); ++i)
{
    do_something(
        layout->itemAt(i)->widget()
    );
}

Note that widget() may return a null pointer.

rocambille
  • 15,398
  • 12
  • 50
  • 68
  • 1
    I know I may be asking much, but could you show me some sample code please? – Wing Jul 20 '16 at 09:45
  • It should be something like that: for (int i = 0; i < layout->count(); ++i) do_something( layout->itemAt(i)->widget() ); Take care the widget() method may return a null pointer. – rocambille Jul 20 '16 at 09:50
  • Thank you very much! You have really been helpful! – Wing Jul 21 '16 at 07:31