Search code examples
qtqmlcontainersstate

QML Detect discarded state of HTML container


I'm using QML with a webengineview container and that webengineview container is killed sometimes (when CPU is high).

My question is, how can I detect with QML the app entered in discarded state for reloading the container?


Solution

  • One way to do it would be like this:

    WebEngineView {
        onLifecycleStateChanged: {
            if (lifecycleState === LifecycleState.Discarded) {
                // Do something
            }
        }
    }
    

    Or if you need to listen for the state from a different object, you can use a Connections object:

    Connections {
        target: myWebEngineView
        onLifecycleStateChanged: {
            if (lifecycleState === LifecycleState.Discarded) {
                // Do something
            }
        }
    }
    

    Or, you could simply bind a property directly to the state and use it however you want to:

    property bool isDiscarded: myWebEngineView.lifecycleState === LifecycleState.Discarded