I would like to know how to get all QPushButtons of a specific Layout.
Here is my code (doesn't work)
QList<QPushButton *> step2PButtons = findChildren<QPushButton *>(step2Layout);
for (auto *button: step2PButtons )
button->setEnabled(false);
I have the following error :
no matching function for call to 'MainWindow::findChildren(QVBoxLayout*&)'
I can get all the elements of my interface, but i cannot get elements of a specific layout.
Thanks for helping
How about this:
QList<QPushButton*> step2PButtons = step2Layout->findChildren<QPushButton*>();
for(auto it = step2PButtons.begin(); it != step2PButtons.end(); ++it)
(*it)->setEnabled(false);
I'm guessing step2Layout
is a pointer to a QVBoxLayout
object.
It's a member function of QObject
, which step2Layout
is derived from. You're calling it from QMainWindow
, and that's why you're getting all Window buttons.
The Quantum Physicist's answer is correct but one small detail is missing: when you create a layout with Qt Designer, if you look at the generated code you will see that it creates a "hidden" QWidget, parent to the layout:
QWidget *verticalLayoutWidget;
QVBoxLayout *verticalLayout;
[...]
verticalLayoutWidget = new QWidget(centralWidget);
Then the button that you think is child of the layout is actually not, it is child of this hidden widget.
pushButton = new QPushButton(verticalLayoutWidget);
If you replace step2Layout->findChildren<QPushButton*>();
by step2Layout->parentWidget()->findChildren<QPushButton*>();
you should get the buttons that are in your layout and can process them in the loop.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.