2

A nice simple example would be nice. For my part, I wish to do something like this:

myLayout->addWidget(myButton1);
myLayout->addWidget(myButton2);

myButtonList<QToolButtons*>->append(myLayout->findChildren(QToolButtons));

myButtonList->at(1)->setText("This is 'myButton2'");
Anon
  • 2,267
  • 3
  • 34
  • 51

2 Answers2

5

A lot easier would be the approach to first fill the list and afterwards the layout:

QList<QToolButton *> list;
list.append(new QToolButton);
list.last().setText("1");

myLayout->addWidget(list.last());

This could then also be easily looped for a higher amount of buttons.

You can still use

ParentWidget->findChildren<QToolButtons *>();

edited given vahancho hint for parent is always a widget, not the layout

Sebastian Lange
  • 3,879
  • 1
  • 19
  • 38
  • Although the contents on my layout changes, you did give me an idea; The signal I use to move widgets onto the layout, I will also have them add to a list. – Anon Sep 16 '13 at 12:07
  • 1
    Is `myLayout->findChildren();` valid call? Isn't `findChildren()` function member of rather QObject than QLayout class? – vahancho Sep 16 '13 at 12:28
  • you're right, the parentwidget needs to be used, not the layout. Thx vahancho – Sebastian Lange Sep 17 '13 at 05:53
5

Use QList along with using findChildren with QWidget because QLayout does not show QWidgets as its children and a QWidget have a parent QWidget only. Refer this

QWidget w;
QPushButton *p1= new QPushButton;
QPushButton *p2= new QPushButton;
QHBoxLayout l;
l.addWidget(p1);
l.addWidget(p2);
w.setLayout(&l);
QList<QPushButton*> buttons = w.findChildren<QPushButton*>();
buttons.at(0)->setText("Hello I am P1"); 
buttons.at(1)->setText("Hello I am P2");
w.show();
Community
  • 1
  • 1
Tab
  • 785
  • 2
  • 11
  • 27