如何给垂直滚动条和滚动区域添加鼠标滚轮滚动?

How to add mousewheel scrolling to the vertical scrollbar or scrolled area?

这是我几个月来第一次再次接触 QML(我主要是 C++ backend/alogrithm 开发人员)为某种调度算法编写一个小型前端

这是我的肮脏的 QtQuick 1.1 图表原型 - 了解我想要达到的目标

架构 of/requirements 甘特图:

这是我目前准备好一切的干净迷你测试

我的rainbows.gml

import QtQuick 2.15
import QtQuick.Controls 2.15
import QtGraphicalEffects 1.0

Item{
    id: myApp
    
    height: 600
    width: 600
    
    Item {
        id: ganttChart
        width: 400
        height: 400
        anchors.centerIn: parent
        clip: true
        
        property real taskNamesWidth: 100
        
        // runtime
        property real myContentHeight: 2 * ganttChart.height
        property real mytaskNamesContentWidth: 200
        property real myTaskRangesContentWidth: 500
        
        component FillRainbowGradient : LinearGradient
        {
            anchors.fill: parent
            start: Qt.point(0, 0)
            end: Qt.point(parent.width, parent.height)
            gradient: Gradient {
                GradientStop { position: 0.000; color: Qt.rgba(1, 0, 0, 1) }
                GradientStop { position: 0.167; color: Qt.rgba(1, 1, 0, 1) }
                GradientStop { position: 0.333; color: Qt.rgba(0, 1, 0, 1) }
                GradientStop { position: 0.500; color: Qt.rgba(0, 1, 1, 1) }
                GradientStop { position: 0.667; color: Qt.rgba(0, 0, 1, 1) }
                GradientStop { position: 0.833; color: Qt.rgba(1, 0, 1, 1) }
                GradientStop { position: 1.000; color: Qt.rgba(1, 0, 0, 1) }
            }
        }
        
        Row
        {
            height: parent.height
            width: parent.width
            
            Item
            {
                id: taskNames
                width: ganttChart.taskNamesWidth
                height: parent.height
                clip: true
                
                Rectangle
                {
                    id: taskNamesContent
                    width: ganttChart.mytaskNamesContentWidth
                    height: ganttChart.myContentHeight
                    
                    FillRainbowGradient {}
                    
                    x: -hbarTaskNames.position * width
                    y: -vbar.position * height
                }
                
                ScrollBar {
                    id: hbarTaskNames
                    hoverEnabled: true
                    active: hovered || pressed
                    orientation: Qt.Horizontal
                    size: taskNames.width / taskNamesContent.width
                    anchors.left: parent.left
                    anchors.right: parent.right
                    anchors.bottom: parent.bottom
                }
            }
            
            Item
            {
                id: taskRanges
                width: parent.width - taskNames.width
                height: parent.height
                clip: true
                
                Rectangle
                {
                    id: taskRangesContent
                    width: ganttChart.myTaskRangesContentWidth
                    height: ganttChart.myContentHeight
                    
                    FillRainbowGradient {}
                    
                    x: -hbarTaskRanges.position * width
                    y: -vbar.position * height
                }
                
                ScrollBar {
                    id: hbarTaskRanges
                    hoverEnabled: true
                    active: hovered || pressed
                    orientation: Qt.Horizontal
                    size: taskRanges.width / taskRangesContent.width
                    anchors.left: parent.left
                    anchors.right: parent.right
                    anchors.bottom: parent.bottom
                }
            }
        }
        
        ScrollBar {
            id: vbar
            hoverEnabled: true
            active: hovered || pressed
            orientation: Qt.Vertical
            size: ganttChart.height / ganttChart.myContentHeight
            anchors.top: parent.top
            anchors.right: parent.right
            anchors.bottom: parent.bottom
        }
    }
}

问题:

允许使用鼠标滚轮进行垂直滚动需要什么? 我是否需要为此使用 ScrollViews 或 ListView,或者是否有附加 MouseHandler 的方法?

感谢您的帮助和提示

你可以使用 MouseArea.

将其放在甘特图上方并使用 wheel 信号滚动垂直滚动条。


您的代码可能如下所示:

MouseArea {
    anchors.fill: ganttChart

    onWheel: {
        if (wheel.angleDelta.y > 0) {
            vbar.decrease()
        }
        else {
            vbar.increase()
        }
    }
}