Search code examples
c++qmlqtwebkitqt-quick

How to use statusChanged in WebView ( QtQuick 2.0 and QtWebKit 3)?


In QtQuick 1.0 and QtWebKit 1.0 i could do onLoadFinished to execute function after page was ready. How to do it in versions 2 and 3 respectively?

There is a statusChanged in docuements. Id don't understand how to use it.

Previously i had:

import QtQuick 2.0
import QtWebKit 3.0

WebView {
    width: 700
    height: 800

    url:"http://www.yahoo.com"
    settings.developerExtrasEnabled : true


    id: webView
    objectName: "myWebView"




    onLoadFinished: evaluateJavaScript("window.setTimeout('window.location.reload()',5000);")

}

but it shows error:
Cannot assign to non-existent property "onLoadFinished"


Solution

  • You have to use the onLoadingChanged signal and loadRequest object to check the exact status: http://qt-project.org/doc/qt-5.0/qtwebkit/qml-qtwebkit3-webview.html#onLoadingChanged-signal

    import QtQuick 2.0
    import QtWebKit 3.0
    
    WebView {
        width: 700
        height: 800
    
        url:"http://google.com"
    
        id: webView
        objectName: "myWebView"
    
        onLoadingChanged: {
            console.log("onLoadingChanged: status=" + loadRequest.status);
            if (loadRequest.status == WebView.LoadStartedStatus) 
                console.log("Loading started...");
            if (loadRequest.status == WebView.LoadFailedStatus) {
               console.log("Load failed! Error code: " + loadRequest.errorCode);
               if (loadRequest.errorCode === NetworkReply.OperationCanceledError)
                   console.log("Load cancelled by user");
            } 
            if (loadRequest.status == WebView.LoadSucceededStatus) 
                console.log("Page loaded!");
        }
    
    }
    

    The onLoadingChanged signal occurs when any page load begins, ends, or fails. Various read-only parameters are available on the loadRequest:

    • url: the location of the resource that is loading.
    • status: Reflects one of three load states: LoadStartedStatus, LoadSucceededStatus, or LoadFailedStatus. See WebView::LoadStatus.
    • errorString: description of load error.
    • errorCode: HTTP error code.
    • errorDomain: high-level error types, one of NetworkErrorDomain, HttpErrorDomain, InternalErrorDomain, DownloadErrorDomain, or NoErrorDomain. See WebView::ErrorDomain.