在QML中,经常会用到ListView控件,我们主要用到MVC模式,下面介绍几种常用数据模型,其中包括QML和C++模型
ListModel:
ListModel是一个简单的ListElement容器,每个容器都包含数据角色。其中内容可以动态定义,也可以在QML中显式定义。
ListModel {
id:m_model
ListElement {
name: "Bill Smith"
number: "555 3264"
color1:"red"
}
ListElement {
name: "John Brown"
number: "555 8426"
color1:"green"
}
ListElement {
name: "Sam Wise"
number: "555 0473"
color1:"blue"
}
}
ListView
{
width: 100
height: 100
model:m_model
delegate: Text{
color: color1
text:name+":"+number}
}
ObjectModel
当ObjectModel被用于视图的时候,视图不再需要委托,因为对象模型已经包含了可视化的委托(项)
ObjectModel {
id: itemModel
Rectangle { height: 20; width: 80;
Text{
color: "red"
text:"Bill Smith"+":"+"555 3264"
}
}
Rectangle { height: 20; width: 80;
Text{
color: "green"
text: "John Brown"+":"+"555 8426"
}
}
Rectangle { height: 20; width: 80;
Text{
color: "blue"
text:"Sam Wise"+":"+"555 0473"
}
}
}
ListView{
width: 100
hei