如何检测我的应用程序在 Qt 中失去焦点? [英] 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.
为此,我重新实现了 leaveEvent()
.这似乎适用于所有情况,除非通过 Alt+Tab
切换到另一个应用程序.我发现我可能需要捕捉另一个事件,但不知何故我找不到合适的事件.你能推荐一个吗?
To do it, I reimplemented leaveEvent()
. This seems to work in all cases except when switching to another application by 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?
推荐答案
您要查找的事件是 QEvent::ApplicationDeactivate
:应用程序已暂停,用户无法使用".
The event you are looking for is QEvent::ApplicationDeactivate
: "The application has been suspended, and is unavailable to the user".
您可以在您的 QApplication
实例上安装一个事件过滤器来捕捉这个事件.请参阅QObject::installEventFilter(QObject*)的文档代码>
了解更多详情.
You can install an event filter on your QApplication
instance to catch this event. See the documentation for QObject::installEventFilter(QObject*)
for more details how this works.
自 Qt 5.2 起,QEvent::ApplicationDeactivate
事件已被弃用.在 Qt 5.2(或更高版本)中识别应用程序何时停用的正确方法是使用 QGuiApplication::applicationStateChanged(Qt::ApplicationState state)
信号.
Since Qt 5.2 the QEvent::ApplicationDeactivate
event is deprecated. 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 中失去焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!