虽然我已经想通了,但还是花了很长时间。如果您知道一个不那么复杂的解决方案 - 请分享。
我想提供一个View适用于任何类型model(带有任意命名的字段)的自定义:
model: 3model: [ "red", "yellow", "green" ]model: [ {color: "red"}, {color: "yellow"}, {color: "green"} ]model: ListModel{ ListElement{color: "red"}; ListElement{color: "green"} }- 子类
QAbstractItemModel等...
为此,我决定property Component delegate为该用户提供一个视觉效果,该视觉效果Item将插入到我的View.
但是,View还需要从中访问一些信息,delegate否则它会促进不必要的代码重复或代理的不必要的复杂性model。
MyView {
mymodel: ["red", "blue"]
mydelegate: Text {
text: "color=" + modelData.name
must_also_somehow_set_color_of_MyView_element_to: modelData.color
}
}
...其中 MyView 应该是这样的:
Column {
Repeater {
model: mymodel
delegate: Rectangle {
color: somehow_get_color_from_mydelegate
Text {} // <--- instantiate mydelegate here with data from mymodel
}
}
}
这样做似乎很容易,但幼稚的尝试并没有奏效。作为答案发布的解决方案对我有用。