How to detect that my application has lost focus in Qt?

I show a popup when the mouse cursor is over certain widgets, and I would like to hide this popup when the mouse leaves the widget.

To do this, I redefined leaveEvent() . This seems to work in all cases except when switching to another application on Alt+Tab . I realized that I probably need to catch another event, but for some reason I can’t find a suitable one. Can you offer one?

+6
source share
1 answer

The event you are looking for is QEvent::ApplicationDeactivate : "The application is paused and inaccessible to the user."

You can install an event filter on a QApplication instance to catch this event. See the documentation for QObject::installEventFilter(QObject*) for more details on how this works.

Since Qt 5.2, the QEvent::ApplicationDeactivate event is deprecated. The correct way to determine when an application is deactivated in Qt 5.2 (or later) is to use the signal QGuiApplication::applicationStateChanged(Qt::ApplicationState state) .

+9
source

All Articles