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

How to use the mouse wheel to slide the QML page based on ScrollBar?

我试图创建一个子 window 来显示 window 无法完全显示的文章。现在,我可以通过拖动 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
        }
    }
}

如有任何帮助,我们将不胜感激!谢谢你。 :)

我已经设法通过将 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
        }
    }
}