首页 > 解决方案 > 如何在 qt 5.9.6 中滚动 webengineview 的位置

问题描述

我正在运行最小的 webengineview 示例,并插入了一个按钮来向下移动页面。代码如下。我尝试使用 scrollPosition 但似乎滚动位置仅用于获取滚动位置。有没有办法将按钮点击链接到页面上的位置?谢谢

import QtQuick 2.0
import QtQuick.Window 2.0
import QtWebEngine 1.3
import QtQuick.Controls 1.4

Window {
     width: 1024
     height: 750
     visible: true

Rectangle {

    anchors.fill: parent


    WebEngineView {
        id:page
        anchors.fill: parent
        url: "http://www.qt.io"
        }

    Button {
        id: buttondown

        anchors {
            bottom: parent.bottom
            horizontalCenter: parent.horizontalCenter
        }

        width: 100
        height: 50
        text: "DOWN"
        onClicked:{
                    page.scrollPosition(1,1)

                }

    }

}

}

标签: qtqmlqwebengineview

解决方案


像这样编辑了 qml 文件,现在页面通常是滚动的。但是,如果要显示的 html 具有 index.html,则页面无法正确滚动。有人知道为什么吗?

import QtQuick 2.0
import QtQuick.Window 2.0
import QtWebEngine 1.3
import QtQuick.Controls 1.4

Window {
    width: 1024
    height: 750
    visible: true

    Rectangle {

        anchors.fill: parent


        WebEngineView {
            id:web
            anchors.fill: parent
            url: "http://www.qt.io"
            }

        Button {
            id: buttondown

            anchors {
                bottom: parent.bottom
                horizontalCenter: parent.horizontalCenter
             }

            width: 100
            height: 50
            text: "DOWN"
            onClicked:{
                    
                web.runJavaScript("window.scrollBy(%1,%2);".arg(0).arg(10));

                }

    }

}

}


推荐阅读