如何将ListModel与SwipeView结合使用?

时间:2019-05-24 19:50:55

标签: qt qml qt-quick qtquickcontrols swipeview

我有一个c ++类,我在qml中注册了该类,并且该类具有一个从QAbstractListModel继承的模型,现在我希望这个模型具有一个SwipeView

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

    }

但是SwipView不具有模型属性吗? id应该如何将此模型动态地添加页面到swipeview?

1 个答案:

答案 0 :(得分:2)

您可以使用中继器作为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
                }
            }
        }
    }
}