Thomas Ayoub
Thomas Ayoub

Reputation: 29441

Equivalent of QEvent ApplicationDeactivate

I use the events QEvent::ApplicationActivate and QEvent::ApplicationDeactivate to show/hide some part of my app when needed.

But now, thoses events are flagged as deprecated :

This enum has been deprecated. Use ApplicationStateChange instead.

So I tried to switch to this event. It is triggered when it should, but I can't find a way to get the application state with some sort of cast or any getters.

Any ideas ?

Upvotes: 1

Views: 1811

Answers (1)

Pavel Strakhov
Pavel Strakhov

Reputation: 40502

Just as for any other event, you need to cast it to more specific type to get event properties. The class is QApplicationStateChangeEvent. The documentation is surprisingly silent about it, but it exists and is declared in event.h. The following code works fine in my Qt 5.1 installation:

#include <QApplicationStateChangeEvent>

bool MainWindow::eventFilter(QObject *o, QEvent *e) {
  if (e->type() == QEvent::ApplicationStateChange) {
    qDebug() << "state:" 
             << static_cast<QApplicationStateChangeEvent*>(e)->applicationState();
  }
  return QMainWindow::eventFilter(o, e);
}

Upvotes: 4

Related Questions