WebEngineHistory QML Type

提供表示网络引擎页面历史的数据模型。更多

Import Statement: import QtWebEngine
Since: QtWebEngine 1.1
In C++: QWebEngineHistory

属性

方法

  • void clear() (since QtWebEngine 1.11)

详细说明

WebEngineHistory 类型可通过WebEngineView.history 属性访问。

WebEngineHistory 类型提供以下WebEngineHistoryModel 数据模型对象:

  • backItems,其中包含已访问页面的 URL。
  • forwardItems包含访问当前页面后访问过的页面的 URL。
  • items包含后退和前进项目的 URL 以及当前页面的 URL。

使用这些模型的最简单方法是在ListView 中使用它们,如下代码片段所示:

ListView {
    id: historyItemsList
    anchors.fill: parent
    model: webEngineView.history.items
    delegate:
        Text {
            color: "black"
            text: model.title + " - " + model.url + " (" + model.offset + ")"
        }
}

ListView 显示相应模型的内容。委托负责列表项的格式。委托中列表中每个项的外观可以单独定义(与网络引擎无关)。

模型角色titleurlicon指定访问页面的标题、URL 和 favicon。偏移量角色指定页面相对于当前页面(0)的位置。正数表示该页面在当前页面之后被访问,负数表示该页面在当前页面之前被访问。

数据模型还可用于创建菜单,如下代码片段所示:

    menuBar: ToolBar {
        id: navigationBar
        RowLayout {
            anchors.fill: parent
            ToolButton {
                enabled: currentWebView && (currentWebView.canGoBack || currentWebView.canGoForward)
                onClicked: historyMenu.open()
                text: qsTr("▼")
                Menu {
                    id: historyMenu
                    Instantiator {
                        model: currentWebView && currentWebView.history.items
                        MenuItem {
                            text: model.title
                            onTriggered: currentWebView.goBackOrForward(model.offset)
                            checkable: !enabled
                            checked: !enabled
                            enabled: model.offset
                        }

                        onObjectAdded: function(index, object) {
                            historyMenu.insertItem(index, object)
                        }
                        onObjectRemoved: function(index, object) {
                            historyMenu.removeItem(object)
                        }
                    }
                }

有关完整示例,请参阅WebEngine Quick Nano 浏览器

另请参阅 WebEngineHistoryModel

属性文档

backItems : WebEngineHistoryModel [read-only]

访问页面的 URL。


forwardItems : WebEngineHistoryModel [read-only]

访问当前页面后访问过的页面的 URL。


items : WebEngineHistoryModel [read-only]

历史记录中的后退项目、前移项目和当前项目的 URL。


方法文档

[since QtWebEngine 1.11] void clear()

清除历史记录。

该方法在 QtWebEngine 1.11 中引入。


© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.