Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?
like image 225
Owen Avatar asked Nov 30 '10 07:11

Owen


2 Answers

You can use the findChild() function with the object name to get a specific child.

You can also use findChildren() to get all the children that have the same name and then iterate through the list using foreach() or QListIterator.

To get a button you can try:

QPushButton* button = pWin->findChild<QPushButton*>("Button name"); 
like image 111
Patrice Bernassola Avatar answered Oct 06 '22 18:10

Patrice Bernassola


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

like image 26
teukkam Avatar answered Oct 06 '22 20:10

teukkam