Вопрос

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'");
Это было полезно?

Решение

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();

Другие советы

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top