在 QML 中使用模型的大小
Use the size of a model in QML
我正在寻找一种方法来访问要在 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
}
}
}
但我不知道如何检索模型的大小。
在文档中我可以找到一个名为 count
的 属性,但没有提示如何访问它。
这取决于您使用的型号。在你的例子中,模型是一个普通的旧 JavaScript 数组,所以你会使用 model.length
。与模型或视图相关的每个其他 Qt 类型都有一个计数 属性:ListModel
、Repeater
、ListView
等。因此,您也可以使用 rep.count
、在你的情况下。
一般方法是请求转发器的基础模型计数 - 在您的情况下它将是 rep.count
。
我正在寻找一种方法来访问要在 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
}
}
}
但我不知道如何检索模型的大小。
在文档中我可以找到一个名为 count
的 属性,但没有提示如何访问它。
这取决于您使用的型号。在你的例子中,模型是一个普通的旧 JavaScript 数组,所以你会使用 model.length
。与模型或视图相关的每个其他 Qt 类型都有一个计数 属性:ListModel
、Repeater
、ListView
等。因此,您也可以使用 rep.count
、在你的情况下。
一般方法是请求转发器的基础模型计数 - 在您的情况下它将是 rep.count
。