【问题标题】:How to Scroll inside ChartView using mousearea in QML如何在 QML 中使用鼠标区域在 ChartView 内滚动
【发布时间】:2018-12-25 17:09:38
【问题描述】:

我编写了这段代码,我希望 ChartView 在鼠标滚轮以一种或另一种方式转动时向右或向左滚动。

ChartView {
    id: chartView
    animationOptions: ChartView.NoAnimation
    theme: ChartView.ChartThemeDark 
    ValueAxis {
        ...
    }
    ValueAxis {
        ...
    }
    LineSeries {
        ...
    }
    MouseArea {
        anchors.fill: parent

        onWheel: {
            if (wheel.modifiers & Qt.ControlModifier){
                if (wheel.angleDelta.y > 0)
                {
                    chartView.scrollRight(5)
                }
                else
                {
                    chartView.scrollLeft(5)
                }
                wheel.accepted=true
            }
            else{
                wheel.accepted=false
            }
        }
    }
}

虽然不工作。 我错过了什么?

【问题讨论】:

    标签: qt qml qquickview


    【解决方案1】:

    我会使用 ScrollView:

    Rectangle {
        width: 200 //size of the viewed part
        height: 200 //size of the viewed part
        color: "transparent"
        clip: true
        ScrollView {
            anchors.fill: parent
            contentWidth: chartRec.width
            contentHeight: chartRec.height
            Rectangle {
                id: chartRec
                height: 400 //size of the chart
                width: 400 //size of the chart 
                color: "transparent"
                ChartView {
                    id: chartView
                    anchors.fill: parent
                    animationOptions: ChartView.NoAnimation
                    theme: ChartView.ChartThemeDark 
                    ValueAxis {
                        ...
                    }
                    ValueAxis {
                        ...
                    }
                    LineSeries {
                        ...
                    }
                }
            }
        }
    }
    

    第二个 Rectangle 的大小应该大于第一个 Rectangle 的大小。如果没有,则不会显示 ScrollView。

    【讨论】:

    • 滚动视图会让我看到绘制在 ChartView 边界之外的点(最小值:最大值)?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多