[英]How to detect that my application lost focus in Qt?
I'm displaying a popup window when the mouse cursor is over a certain widget and I'd like to hide this popup when the mouse leaves the widget. 当鼠标光标在某个小部件上时,我正在显示一个弹出窗口,我想在鼠标离开小部件时隐藏这个弹出窗口。
To do it, I reimplemented leaveEvent()
. 为此,我重新实现了
leaveEvent()
。 This seems to work in all cases except when switching to another application by Alt+Tab
. 这似乎适用于所有情况,除非通过
Alt+Tab
切换到另一个应用程序。 I figured out that I probably need to catch another event, but somehow I can't find the proper one. 我发现我可能需要抓住另一个事件,但不知怎的,我找不到合适的事件。 Can you suggest one?
你能建议吗?
The event you are looking for is QEvent::ApplicationDeactivate
: "The application has been suspended, and is unavailable to the user". 您正在寻找的事件是
QEvent::ApplicationDeactivate
:“应用程序已被暂停,并且对用户不可用”。
You can install an event filter on your QApplication
instance to catch this event. 您可以在
QApplication
实例上安装事件过滤器以捕获此事件。 See the documentation for QObject::installEventFilter(QObject*)
for more details how this works. 有关其工作原理的更多详细信息,请参阅
QObject::installEventFilter(QObject*)
的文档。
Since Qt 5.2 the QEvent::ApplicationDeactivate
event is deprecated. 从Qt 5.2开始,不推荐使用
QEvent::ApplicationDeactivate
事件。 The correct way to identify when an application is deactivated in Qt 5.2 (or later) is to use the QGuiApplication::applicationStateChanged(Qt::ApplicationState state)
signal. 在Qt 5.2(或更高版本)中识别应用程序何时停用的正确方法是使用
QGuiApplication::applicationStateChanged(Qt::ApplicationState state)
信号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.