ObjectModel QML Type
定义一组用作模型的项目。更多
Import Statement: | import QtQml.Models |
属性
- count : int
附属物业
- index : int
方法
- append(object item)
- clear()
- object get(int index)
- insert(int index, object item)
- move(int from, int to, int n)
- remove(int index, int n)
详细说明
ObjectModel 包含要在视图中使用的可视化项目。在视图中使用 ObjectModel 时,由于 ObjectModel 已包含可视化委托(项目),因此视图不需要委托。
项目可通过index 附加属性确定其在模型中的索引。
下面的示例在ListView 中放置了三个彩色矩形。
import QtQuick 2.0 import QtQml.Models 2.1 Rectangle { ObjectModel { id: itemModel Rectangle { height: 30; width: 80; color: "red" } Rectangle { height: 30; width: 80; color: "green" } Rectangle { height: 30; width: 80; color: "blue" } } ListView { anchors.fill: parent model: itemModel } }
另请参阅 Qt Quick 示例 - 视图。
属性文档
count : int |
模型中项目的数量。此属性为只读。
附加属性文档
ObjectModel.index : int |
此附加属性保存模型中该委托项的索引。
它附加到委托的每个实例。
方法文档
append(object item) |
object get(int index) |
返回模型中位于index 的项目。这样就可以通过 JavaScript 访问或修改该项目:
Component.onCompleted: { objectModel.append(objectComponent.createObject()) console.log(objectModel.get(0).objectName); objectModel.get(0).objectName = "first"; }
index 必须是列表中的一个元素。
另请参阅 append() 。
insert(int index, object item) |
移动n 项目from 一个位置to 另一个位置。
from 和 to 的范围必须存在;例如,将前 3 个条目移动到模型的末尾:
objectModel.move(0, objectModel.count - 3, 3)
另请参阅 append().
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.