9

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(). 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?

Daniel Hedberg
  • 5,677
  • 4
  • 36
  • 61
Michal
  • 565
  • 5
  • 15

1 Answers1

12

The event you are looking for is QEvent::ApplicationDeactivate: "The application has been suspended, and is unavailable to the user".

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.

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.

Daniel Hedberg
  • 5,677
  • 4
  • 36
  • 61
  • 3
    Thanks. Inspired by your suggestion, I found that it was enough to catch both QEvent::Leave and QEvent::WindowDeactivate events in my eventFilter and hide my popup on either of these. – Michal Jan 04 '13 at 16:12