一、描述
对象模型ObjectModel用于在视图中显示可视项目,该类型可以将Qt Quick中的可视化项目作为数据项显示到视图上。
使用ObjectModel的视图不需要指定委托,ObjectModel已经包含了可视化的委托。
可使用model的附加属性index获取数据项的索引位置。
二、属性
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)
四、代码示例
import QtQuick
import QtQuick.Controls
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
ObjectModel {
id: iObjectModel
Rectangle { width: 150; height: 50; color: "limegreen"; radius: 8 }
Label { width: 150; height: 50; color: "limegreen"; text: "This is a Label"; font.pixelSize: 36 }
Button { width: 150; height: 50; text: "This is a Button"; font.pixelSize: 16 }
Row {
spacing: 15
Switch { text: checked ? "已打开" : "已关闭" }
Switch { text: checked ? "已打开" : "已关闭" }
Switch { text: checked ? "已打开" : "已关闭" }
}
ScrollView {
width: 180
Label { text: "This is a ScrollView!"; font.pixelSize: 48 }
}
}
ListView {
width: parent.width / 2
height: parent.height
anchors.horizontalCenter: parent.horizontalCenter
spacing: 20
model: iObjectModel
}
}