[英]How to get all QPushButton of a specific layout
I would like to know how to get all QPushButtons of a specific Layout. 我想知道如何获取特定布局的所有QPushButtons。
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. 我猜
step2Layout
是指向QVBoxLayout
对象的指针。
It's a member function of QObject
, which step2Layout
is derived from. 它是
QObject
的成员函数, step2Layout
是从其派生的。 You're calling it from QMainWindow
, and that's why you're getting all Window buttons. 您是从
QMainWindow
调用它的,这就是为什么要获取所有“窗口”按钮的原因。
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: Quantum Physicist的答案是正确的,但缺少一个小细节:当您使用Qt Designer创建布局时,如果查看生成的代码,您会发现它创建了布局的父级“隐藏” QWidget:
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*>();
如果替换
step2Layout->findChildren<QPushButton*>();
by step2Layout->parentWidget()->findChildren<QPushButton*>();
通过
step2Layout->parentWidget()->findChildren<QPushButton*>();
you should get the buttons that are in your layout and can process them in the loop. 您应该获得布局中的按钮,并可以在循环中对其进行处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.