qtqmlqt-quickqtquickcontrolsswipeview

How Use ListModel with SwipeView?


I have a c++ class that i registerd it in qml , and this class have an model that was inherited from QAbstractListModel,Now I want this model with a SwipeView

Manager {
    id: manager
}
SwipeView {
    id: sv           
    model:manager.listModel /// but it don't have model property
}

but SwipView don't havd a model property? How should id add Pages dynamically to thsi swipeview along with this model?


Solution

  • You can use a Repeater as an example of the docs:

    import QtQuick 2.12
    import QtQuick.Window 2.12
    import QtQuick.Controls 2.5
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
        ListModel{
            id: mymodel
            ListElement{
                name: "name1"
                background: "red"
            }
            ListElement{
                name: "name2"
                background: "salmon"
            }
            ListElement{
                name: "name2"
                background: "gray"
            }
        }
        SwipeView{
            id: view
            anchors.fill: parent
            Repeater{
                model: mymodel
                Rectangle{
                    color: model.background
                    Text {
                        anchors.centerIn: parent
                        text: model.name
                    }
                }
            }
        }
    }