3

I am running my Qt application on Qt 5.9.3 on Android and iOS. I want to listen to QGuiApplication::applicationStateChanged directly on a QML file

How can I listen to application state changes on QML using connection without having to write any code to emit signals from my C++ classes.

I want to listen to Qt::ApplicationState::ApplicationInactive and Qt::ApplicationState::ApplicationActive events using a connection. Using something like this in my QML file.

Connections {
   target: something_but_what
   onApplicationStateChanged: {
     console.log("State changed", state)
   }
}

How can I achieve this?

TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159

1 Answers1

7

You have access to that functionality from the Qt object in QML:

  Connections {
    target: Qt.application
    onStateChanged: console.log(Qt.application.state)
  }
dtech
  • 47,916
  • 17
  • 112
  • 190