gpt4 book ai didi

qt - 扩展/连接多个 ListModel 的声明方式

转载 作者:行者123 更新时间:2023-12-04 13:19:35 26 4
gpt4 key购买 nike

我想构建多个 ListModel,它们都将以公共(public)元素开头。我想填充另一个 ListModel 中的剩余元素。

例子:列表模型(一):

COMMON A
COMMON B
COMMON C
A1
B1
C1

列表模型(2):

COMMON A
COMMON B
COMMON C
A2
B2
C2

有没有办法以声明方式“合并”或扩展两个 ListModel

到目前为止,我正在动态地进行:

import QtQuick 2.9
import QtQuick.Window 2.2

Window {
width: 480
height: 320
visible: true

ListModel {
id: commonModel
ListElement { value: "COMMON A" }
ListElement { value: "COMMON B" }
ListElement { value: "COMMON C" }
}

ListModel {
id: model1
ListElement { value: "A1" }
ListElement { value: "B1" }
ListElement { value: "C1" }
}

ListModel {
id: model2
ListElement { value: "A2" }
ListElement { value: "B2" }
ListElement { value: "C2" }
}

ListView {
anchors.fill: parent
model: commonModel // here I would like to directly specify the "concatenated" model

delegate: Text { text: model.value}

// dynamic part that I want to avoid
readonly property var additionalModel: model2
Component.onCompleted: {
for (var i = 0; i < additionalModel.count; ++i) {
var elt = additionalModel.get(i)
model.append(elt)
}
}
}
}

编辑:换句话说,我想做一些像这样简单的事情:

// MyModel.qml
ListModel {
ListElement { value: "COMMON A" }
ListElement { value: "COMMON B" }
ListElement { value: "COMMON C" }
}

能够根据需要在另一个组件中扩展它:

ListView {
model: MyModel {
// keep original elements from MyModel and add custom "component-related" elements
ListElement { value: "OTHER A" }
ListElement { value: "OTHER B" }
ListElement { value: "OTHER C" }
}
}

但这似乎不可能,因为我收到以下错误:

Cannot assign to non-existent default property

最佳答案

让我们从修补错误开始。它来自这里:

model: MyModel {
ListElement { role_value: "OTHER A" }
ListElement { role_value: "OTHER B" }
ListElement { role_value: "OTHER C" }
}

要获得这种行为,您需要设置 MyModel 的属性之一。到 default property .平凡的,像

//  MyModel.qml
ListModel {
default property list<ListElement> otherModels

ListElement { role_value: "COMMON A" }
ListElement { role_value: "COMMON B" }
ListElement { role_value: "COMMON C" }
}

// Main.qml
import QtQuick 2.0
import QtQuick.Window 2.2

Window {
ListView {
model: MyModel {
// these should be auto-assigned to the default property
ListElement { role_value: "OTHER A" }
ListElement { role_value: "OTHER B" }
ListElement { role_value: "OTHER C" }
}
}
}

通常,对于其他类型,这可以正常工作。但是ListElement是一个特例。如果你尝试运行上面的代码,你可能会得到一个错误:

Cannot assign to non-existent property "role_value"

Documentation暗示 ListElements只能在 ListModels 下定义.据我所知,这似乎是唯一可行的解​​决方法。这意味着将默认属性更改为 list<ListModel>并将用法更改为

ListView {
model: MyModel {
// ListModel is auto-assigned to the default property
ListModel {
ListElement { role_value: "OTHER A" }
ListElement { role_value: "OTHER B" }
ListElement { role_value: "OTHER C" }
}
}
}

这是一个最小的完整示例:

//  MyModel.qml
import QtQuick 2.0

ListModel {
id: model
default property list<ListModel> otherModels

ListElement { role_value: "COMMON A" }
ListElement { role_value: "COMMON B" }
ListElement { role_value: "COMMON C" }

// called everytime a model is append to otherModels
onOtherModelsChanged: {
// add ListElements from the last otherModels to this model
var i = otherModels.length - 1;
if (i < 0) return;

for (var j = 0; j < otherModels[i].count; j++)
model.append(otherModels[i].get(j));
}
}


// Main.qml
import QtQuick 2.6
import QtQuick.Window 2.2 // Window
import QtQuick.Controls 2.2 // ItemDelegate

Window {
visible: true
width: 640
height: 480

ListView {
anchors.fill: parent
model: MyModel {
ListModel {
ListElement { role_value: "OTHER A" }
ListElement { role_value: "OTHER B" }
ListElement { role_value: "OTHER C" }
}
}

delegate: ItemDelegate {
text: role_value
width: parent.width
}
}
}

See a more sophisticated demo on Github.

default property list<ListModel>和执行 onOtherModelsChanged允许将多个模型“连接”到基础模型上。

成功截图:

enter image description here

augre 所述,在仅连接一个 模型的情况下,您可以将属性简化为 default property ListModel otherModel并且有

onOtherModelChanged: {
if (otherModel === undefined) return;
for (var i = 0; i < otherModel.count; i++)
model.append(otherModel.get(i));
}

关于qt - 扩展/连接多个 ListModel 的声明方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55355231/

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