我需要创建一个ListModel静态包含对象(字符串和bool)的.如果我ListModel使用append 添加到空元素 - 一切正常.
property ListModel qwe: ListModel {}
var imageToAdd { value: "picture.png", imageType: 1 }
qwe.append({
text: "TextToAdd",
image: imageToADD,
position: 1
})
// This works correct
Run Code Online (Sandbox Code Playgroud)
但我需要ListModel静态创建它并不起作用.
ListModel {
ListElement {
text: "TextToAdd"
image: { value: "Qwer.png", imageType: 1 } // <-- This doesn't work
position: 1
}
}
Run Code Online (Sandbox Code Playgroud)
应该怎么样?
一个ListElementQt中必须有类型的值string,bool,numbers或enum.不允许使用更复杂的数据类型,如hashmaps.
您可以在Qt 5.2源代码中深入阅读:qqmllistmodel.cpp.自Qt 4.7次以来,这没有改变.
列表元素在ListModel定义中定义,并表示将使用ListView或Repeater项目显示的列表中的项目.
列表元素的定义与其他QML元素类似,只是它们包含角色定义的集合而不是属性.使用与属性定义相同的语法,角色既定义数据的访问方式,又包括数据本身.
用于角色的名称必须以小写字母开头,并且应该与给定模型中的所有元素共用.值必须是简单的常量; 字符串(引用并且可选地在对QT_TR_NOOP的调用中),布尔值(true,false),数字或枚举值(例如AlignText.AlignHCenter).
然而,ListModel似乎是能够存储在ECMA-262标准定义的所有类型:原语类型,即Undefined,Null,Boolean,Number,和String以及所述Object类型.
编辑:如果要在QML中创建元素,则必须将代码重写为类似的内容
ListModel {
ListElement {
text: "TextToAdd"
imageValue: "Qwer.png"
imageType: 1
position: 1
}
}
Run Code Online (Sandbox Code Playgroud)
Edit2:或者你采用Javascript方式.首先创建一个空模型并在开始时填充它
ListView {
model: ListModel { id: qwe }
delegate: ...
Component.onCompleted: {
qwe.append({
text: "Image 1",
image: { value: "picture.png", imageType: 1 },
position: 1
});
qwe.append({
text: "Image 2",
image: { value: "picture.png", imageType: 1 },
position: 2
});
qwe.append({
text: "Image 1",
image: { value: "picture.png", imageType: 1 },
position: 3
});
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4362 次 |
| 最近记录: |