Search code examples
androidqtqmlqtquick2qtquickcontrols

Call a function when screen is unlocked


I have opened and focused app on android phone, and I lock the screen. When I unlock it, I want to call a specific function.

I was thinking about Qt.application.state, but how to call function when it has changed?


Solution

  • You searching for void QGuiApplication::applicationStateChanged(Qt::ApplicationState state).

    From Qt documentation:

    void QGuiApplication::applicationStateChanged(Qt::ApplicationState state)

    This signal is emitted when the state of the application changes.

    This function was introduced in Qt 5.2.

    Just connect to this signal at your C++ part of code, check state == Qt::ApplicationActive and trigger some function inside you QML part.