如何在 Qt 中获取小部件的子级?
我通过 Qt 的 KeyPress 函数模拟应用程序的按键。所有按键都工作正常。但是,当我传递一个 QT::Key_Enter
(应该按下当前活动窗口的“确定”按钮)或 QT::Key_Cancel
作为取消按钮时,它什么也不做。
我想也许是因为这些按钮没有焦点,而父窗口本身有焦点。如何获取窗口的子项?或者找到它上面的“确定”或“取消”按钮,以便您可以将其设置为活动窗口,然后成功传递按键?
我有:
QWidget *pWin = QApplication::activeWindow;
QObjectList *pList = pWin->children();
//how do you iterate through the list and find the OK or Cancel button?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
findChild()
函数与对象名称来获取特定的子对象。您还可以使用
findChildren()
获取所有具有相同名称的子项,然后使用foreach()
或QListIterator
。要获得按钮,您可以尝试:
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 usingforeach()
orQListIterator
.To get a button you can try:
您可能希望在小部件上放置一个自定义事件过滤器来捕获关键事件看看到底发生了什么。
You might want to put a custom event filter on your widget to capture the key event and see what really happens to it.