在QML中使用模型的大小

der*_*erM 4 qt qml qtquick2

我正在寻找一种方法来访问QML中模型中可以找到的元素数量.

例如:

Item {
    id: root
    Row {
        id: row
        height: root.height
        width: root.width
        spacing: 5
        Repeater {
            id: rep
            width: root.width
            height: root.height
            model: [5, 3, 3, 1, 12]
            delegate: myDelegate
            Text {
                id: myDelegate
                text: "Element " + index + " of " size + ":" + modelData
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但我无法弄清楚如何检索模型的大小.在文档中,我可以找到一个名为的属性count,但没有提示如何访问它.

Mit*_*tch 9

这取决于您使用的型号.在您的情况下,该模型是一个普通的旧JavaScript数组,所以你可以使用model.length.相关模型或视图所有其他的Qt类型都有一个Count属性:ListModel,Repeater,ListView等,所以,你也可以使用rep.count,你的情况.