Search code examples
qtqmlqwebengineview

WebEngineView variables 'canGoBack' and 'canGoForward' returns wrong values


I'm working on an application that's running on Raspberry Pi 4, and I got this strange behavior of QML's WebEngineView.

I created 3 buttons for going to the main page and for going back and forward:

  Button {
        id: box_button_main_page
        text: "Go to Main Page"
        onClicked: mainWebView.url = "https://www.youtube.com/"
    }
    Button {
        id: box_button_go_back
        enabled: mainWebView.canGoBack
        text: "Back"
        onClicked: mainWebView.goBack()
    }

    Button {
        id: box_button_go_forward
        enabled: mainWebView.canGoForward
        text: "Forward"
        onClicked: mainWebView.goForward()
    }

It works as expected: the "Back" and "Forward" buttons become grayed at the right points. But, if one makes below actions:

  • go to any link
  • go to yet another link
  • click "Back" button
  • click "Go to Main Page"

then both "Back" and "Forward" buttons are still enabled. And clicking on "Forward" button does nothing.

I checked in the "Go to Main Page" onClicked action that both canGoBack and canGoForward returns true after setting url property. I tried to search if there is yet another method to order WEV to switch to another page, but there is none :(.

I didn't find on the Net anyone reporting it as a bug, so either I'm doing it wrong, or I actually found a bug >_>


Solution

  • It works with WebAction. Have a look at this demo. All I've done is add the following Button in the ToolBar RowLayout:

    Button {
        text: "Go to Main Page"
        onClicked: webEngineView.url = "https://www.youtube.com/"
    }
    

    enter image description here


    Here is a full example using WebAction:

    import QtQuick 2.15
    import QtQuick.Window 2.15
    import QtWebEngine 1.10
    import QtQuick.Controls 2.15
    import QtQuick.Layouts 1.15
    
    ApplicationWindow {
        id: window
        visible: true
        width: 800
        height: 600
        title: qsTr("WebEngineAction Example")
    
        header: ToolBar {
            RowLayout {
                anchors.fill: parent
    
                Button {
                    text: "Go to Main Page"
                    onClicked: webEngineView.url = "https://www.youtube.com/"
                }
    
                Button {
                    property int itemAction: WebEngineView.Back
                    text: "Back"
                    enabled: webEngineView.action(itemAction).enabled
                    onClicked: webEngineView.action(itemAction).trigger()
                }
    
                Button {
                    property int itemAction: WebEngineView.Forward
                    text: "Forward"
                    enabled: webEngineView.action(itemAction).enabled
                    onClicked: webEngineView.action(itemAction).trigger()
                }
            }
        }
    
        WebEngineView {
            id: webEngineView
            url: "https://qt.io"
            anchors.fill: parent
        }
    }