你如何得到一个小部件的孩子在Qt? [英] How do you get a widget's children in Qt?
问题描述
我通过Qt的KeyPress函数模拟keyPresses到应用程序。所有的KeyPresses工作正常。但是,当我传递一个 QT :: Key_Enter
,它应该按下当前活动窗口的确定按钮,或 QT :: Key_Cancel
为取消按钮,它什么也不做。
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.
我想也许,因为这些按钮没有焦点,而父窗口本身有它。你怎么得到窗口的孩子?或者找到OK或Cancel按钮,这样你可以将其设置为activeWindow然后成功传递KeyPresses?
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?
我有:
QWidget *pWin = QApplication::activeWindow;
QObjectList *pList = pWin->children();
//how do you iterate through the list and find the OK or Cancel button?
推荐答案
您可以使用 findChild
函数与对象名称以获取特定儿童。
您可以使用 findChildren
来获取所有具有相同名称的子元素,并使用 foreach
或 QListIterator
。
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");
这篇关于你如何得到一个小部件的孩子在Qt?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!