gpt4 book ai didi

qml - 将静态对象添加到 ListModel

转载 作者:行者123 更新时间:2023-12-04 17:35:37 24 4
gpt4 key购买 nike

我需要创建一个 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

但我需要创建 ListModel静态的,它不起作用。
ListModel {
ListElement {
text: "TextToAdd"
image: { value: "Qwer.png", imageType: 1 } // <-- This doesn't work
position: 1
}
}

它应该是什么样子?

最佳答案

一个 ListElement在 Qt 中必须具有类型 string 的值, bool , numbersenum .不允许使用更复杂的数据类型,例如哈希图。

您可以在 Qt 5.2 源代码中深入阅读此内容:qqmllistmodel.cpp .自 Qt 4.7 times 以来,这没有改变.

List elements are defined inside ListModel definitions, and represent items in a list that will be displayed using ListView or Repeater items.

List elements are defined like other QML elements except that they contain a collection of role definitions instead of properties. Using the same syntax as property definitions, roles both define how the data is accessed and include the data itself.

The names used for roles must begin with a lower-case letter and should be common to all elements in a given model. Values must be simple constants; either strings (quoted and optionally within a call to QT_TR_NOOP), boolean values (true, false), numbers, or enumeration values (such as AlignText.AlignHCenter).



然而,一个 ListModel似乎能够存储 ECMA-262 标准中定义的所有类型:原始类型,即 Undefined , Null , Boolean , Number , 和 String以及 Object类型。

编辑:如果要在 QML 中创建元素,则必须将代码重写为类似
ListModel {
ListElement {
text: "TextToAdd"
imageValue: "Qwer.png"
imageType: 1
position: 1
}
}

编辑 2:或者你去 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
});
}
}

关于qml - 将静态对象添加到 ListModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20537417/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com