如何在 Qt 中获取小部件的子级?

发布于 2024-10-05 06:52:02 字数 433 浏览 12 评论 0原文

我通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事还在继续 2024-10-12 06:52:02

您可以使用 findChild()函数与对象名称来获取特定的子对象。

您还可以使用 findChildren() 获取所有具有相同名称的子项,然后使用 foreach()QListIterator

要获得按钮,您可以尝试:

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

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");
猥琐帝 2024-10-12 06:52:02

您可能希望在小部件上放置一个自定义事件过滤器来捕获关键事件看看到底发生了什么。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文