How do you get a widget's children in Qt?

こ雲淡風輕ζ 提交于 2019-12-05 09:25:09

问题


I'm simulating keyPresses to an application through Qt's KeyPress function. All the KeyPresses work fine. However when I pass a QT::Key_Enter which is supposed to press the OK button of the currently active window, or QT::Key_Cancel for the cancel button, it does nothing.

I'm thinking maybe, because these buttons don't have the focus, and the parent window itself has it. How do you get the children of a window? or rather find the OK or Cancel button on it so you could set it as the activeWindow and then pass KeyPresses successfully?

I have:

QWidget *pWin = QApplication::activeWindow;
QObjectList *pList = pWin->children();
//how do you iterate through the list and find the OK or Cancel button?

回答1:


You can use the findChild function with the object name to get a specific children. You can use too findChildren to get all children that have the same name and the iterate through the list using foreach or QListIterator.

To get a button you can try:

QPushButton* button = pWin->findChild<QPushButton*>("Button name");



回答2:


You might want to put a custom event filter on your widget to capture the key event and see what really happens to it.



来源:https://stackoverflow.com/questions/4311352/how-do-you-get-a-widgets-children-in-qt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!