QML - Vertical Swipe View?

2019-07-17 09:37发布

问题:

Is it possible to use the SwipeView in QML for vertical swiping rather than horizontal?

I'd like the Page contents of a SwipeView to float vertically, so a user has to scroll up and down to navigate between Pages.

If this is not possible how would I go about it?

UPDATE This functionality has been added to QT as seen here:

http://doc-snapshots.qt.io/qt5-dev/qml-qtquick-controls2-swipeview.html#orientation-prop

回答1:

After some more research I came up with a solution that works fine, using a ListView/ListModel/ListDelegate - posting it here for others who wants to achieve the same.

QML:

ListView {
    snapMode: ListView.SnapOneItem
    highlightRangeMode: ListView.StrictlyEnforceRange

    anchors {
        top: parent.top
        bottom: parent.bottom
        left: parent.left
        right: parent.right
    }

    model: ListModel {
        id: listModel

        ListElement {
            text: "1"
        }
        ListElement {
            text: "2"
        }
        ListElement {
            text: "3"
        }
    }

    delegate: Page {
        width:  ListView.view.width
        height: ListView.view.height

        Text {
            anchors.centerIn: parent
            text: model.text
        }
    }
}