Qt:如何获取当前正在运行的窗口? [英] Qt: How do I get the currently running window?

查看:174
本文介绍了Qt:如何获取当前正在运行的窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个模拟按键的测试应用程序,我想获得每次按键后显示的窗口.这是代码块.

I'm writing a test app which simulates key presses and I would like to get what window is displayed after each key presses. Here's the code block.

std::auto_ptr<MyForm> pForm(new MyForm(3,3)); 
QTest::keyPress(pForm.get(), Qt::Key_0); 

在此处按 0 后,会出现一个窗口,我想检查它是哪个窗口,以便稍后进行 QCompare/评估.

After pressing 0 here, A window is gonna show up and I would like to check what window it is so I could QCompare/evaluate it later.

有什么想法吗?

更新:

我在使用时遇到分段错误

I'm getting a segmentation fault when I use

std::auto_ptr<MyForm> pForm(new MyForm(3,3)); 
QTest::keyPress(pForm.get(), Qt::Key_0); 
QWidget *pWin = QApplication::activeWindow();
QCOMPARE(pWin->windowTitle(), QString("My Second Menu"));

推荐答案

如果你的所有窗口都是通过你的应用程序创建的,你可以使用 QApplication 类.例如,activeWindow()函数返回具有输入焦点的小部件.但是还有很多其他功能可以帮助您.

If all your windows have been created through your application, you can use the QApplication class. By example, the activeWindow() function returns the widget that have the input focus. But there's a lot of other functions that can help you.

希望有帮助

这篇关于Qt:如何获取当前正在运行的窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆