【问题标题】:WebEngineView variables 'canGoBack' and 'canGoForward' returns wrong valuesWebEngineView 变量“canGoBack”和“canGoForward”返回错误值
【发布时间】:2021-03-04 11:47:03
【问题描述】:

我正在开发一个在 Raspberry Pi 4 上运行的应用程序,我发现 QML 的 WebEngineView 出现这种奇怪的行为。

我创建了 3 个按钮用于转到主页和返回和前进:

  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()
    }

它按预期工作:“后退”和“前进”按钮在正确的位置变为灰色。但是,如果有人进行以下操作:

  • 转到任何链接
  • 转到另一个链接
  • 点击“返回”按钮
  • 点击“转到主页”

那么“后退”和“前进”按钮仍处于启用状态。然后点击“转发”按钮什么也不做。

我在“转到主页”onClicked 操作中检查了canGoBackcanGoForward 在设置url 属性后返回true。我试图搜索是否还有另一种方法可以命令 WEV 切换到另一个页面,但是没有:(。

我没有在网上找到任何人将其报告为错误,所以要么我做错了,要么我真的发现了一个错误 >_>

【问题讨论】:

  • 我创建了一个错误报告。也许您可以使用navigationHistory 属性自己编写功能。

标签: qt qml qwebengineview


【解决方案1】:

它适用于WebAction。看看这个demo。我所做的只是在ToolBar RowLayout 中添加以下Button

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


这是一个使用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
    }
}

【讨论】:

  • 我们花了很长时间才做出回应,与此同时,我们的团队找到了另一个解决方案,但您的解决方案要好得多。谢谢!
猜你喜欢
  • 2012-02-07
  • 2012-03-24
  • 1970-01-01
  • 2021-10-24
  • 2018-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多