Owe*_*wen 2 c++ qt widget button
我正在尝试获取窗口的所有按钮子窗口小部件。这些按钮是通过QDialogButtonBox创建的。如何获取取消/确定/保存按钮?
我有:
QWidget *pWin = QApplication::activeWindow();
QList<QPushButton *> allPButtons = pWin->findChildren<QPushButton *>();
QListIterator<QPushButton*> i(allPButtons);
while( i.hasNext() )
{
//identify which button is cancel/ok/save button here
/*Note: This is where I'm having trouble, getting the text of the
button here returns NULL. Any other way of Identifying which is
which?
Is it a special case when buttons are created through QDialogButtonBox?
*/
}
Run Code Online (Sandbox Code Playgroud)
您应该使用QDialogButtonBox::button()方法来获取相应角色的按钮。
例如 :
QPushButton* pOkButton = pButtonBox->button(QDialogButtonBox::Ok);
QPushButton* pCancelButton = pButtonBox->button(QDialogButtonBox::Cancel);
// and so on...
Run Code Online (Sandbox Code Playgroud)
一般来说,我认为从文本中查找按钮是一个坏主意,因为当您的应用程序国际化时,该文本可能会发生变化。