如何使用鼠标滚轮基于ScrollBar滑动QML页面?

时间:2020-10-16 05:32:54

标签: qt qml qtquick2

我试图创建一个子窗口来显示窗口无法完全显示的文章。现在,我可以通过拖动 ScrollBar (QML类型)来滑动页面,但是我也想使用鼠标滚轮来滑动页面。

代码如下:

import QtQuick 2.2
import QtQuick.Window 2.12
import QtQuick.Controls 2.12

Window {
    id:privacyWindow
    width: 640
    height: 480
    title:qsTr("Privacy")

    Rectangle {
        id: privacy
        clip: true
        width: privacyWindow.width
        height: privacyWindow.height
        anchors.centerIn: parent

        Rectangle {
            id: textArea
            clip: true
            width: privacyWindow.width - 150
            height: privacyWindow.height
            anchors.centerIn: parent

            Text {
                id: content
                width: textArea.width
                text: ""
                wrapMode: Text.WordWrap
                textFormat: Text.RichText
                //x: -horizontalBar.position * width
                y: -verticalBar.position * height
            }
        }

        ScrollBar {
            id: verticalBar
            hoverEnabled: true
            active: hovered || pressed
            orientation: Qt.Vertical
            size: privacy.height / content.height
            anchors.top: parent.top
            anchors.right: parent.right
            anchors.bottom: parent.bottom
        }
    }
}

任何帮助将不胜感激!谢谢。 :)

1 个答案:

答案 0 :(得分:0)

我设法通过将 Rectangle 替换为 Flickable 来解决此问题。

import QtQuick.Controls 2.12

Window {
    id: privacyWindow
    width: 640
    height: 480
    title:qsTr("Privacy")

    Flickable {
        id: flickable
        clip: true
        width: privacyWindow.width - 150
        height: privacyWindow.height
        contentWidth: content.width
        contentHeight:content.height
        anchors.centerIn: parent

        Text {
            id: content
            width: flickable.width
            text: ""
            wrapMode: Text.WordWrap
            textFormat: Text.RichText
        }

        ScrollBar.vertical: ScrollBar {
            parent: flickable.parent
            anchors.top: parent.top
            anchors.right: parent.right
            anchors.bottom: parent.bottom
        }
    }
}