如何在水平QML滚动视图上使用滚动

时间:2019-02-09 18:56:52

标签: qt

我想用鼠标滚动在水平滚动视图上滚动。我如何做到这一点,这是示例滚动视图的代码。

        ScrollView {
        id:scrollview

        ListView {
            anchors.fill: parent
            model: 30
            orientation: ListView.Horizontal
            delegate: Component{

                Item{
                    height: scrollview.height
                    width: scrollview.height

                    Rectangle{
                        anchors.fill: parent
                        color:"lightgrey"
                        Text{
                            anchors.centerIn: parent
                            text: index
                        }
                    }

                }
            }
        }
    }

我想像在垂直视图中那样实现滚动,而只是在水平方向上实现

1 个答案:

答案 0 :(得分:0)

我了解到它可以与ScrollView顶部的MouseArea一起使用,该MouseArea侦听鼠标滚轮事件,但会传递其他事件。

ScrollView {
    id:scrollview

    property int scrollSpeed: 30

    ListView {

        anchors.fill: parent
        model: 30
        orientation: ListView.Horizontal
        delegate: Component {

            Item {
                height: scrollview.height
                width: scrollview.height

                Rectangle {
                    anchors.fill: parent
                    color:"lightgrey"
                    Text {
                        anchors.centerIn: parent
                        text: index
                    }
                }

            }
        }
    }

}

MouseArea {
    anchors.fill: scrollview

    onWheel: {
        if (wheel.angleDelta.y > 0) {
            scrollview.flickableItem.contentX -= scrollview.scrollSpeed;
            if (scrollview.flickableItem.contentX < 0) {
                scrollview.flickableItem.contentX = 0;
            }
        } else {
            scrollview.flickableItem.contentX += scrollview.scrollSpeed;
            if (scrollview.flickableItem.contentX + scrollview.flickableItem.width > scrollview.flickableItem.contentWidth) {
                scrollview.flickableItem.contentX = scrollview.flickableItem.contentWidth -  scrollview.flickableItem.width;
            }
        }
    }
    onClicked: mouse.accepted = false;
    onPressed: mouse.accepted = false;
    onReleased: mouse.accepted = false;
    onDoubleClicked: mouse.accepted = false;
    onPositionChanged: mouse.accepted = false;
    onPressAndHold: mouse.accepted = false;
}