[英]How to assign custom object to sourceItem?
A would like to visualize MapQuickItem depend on if condition. 想要可视化MapQuickItem取决于条件。
I have two custom objects ClusterMarker which is a Rectangle and PromotionMarker which is an Image object. 我有两个自定义对象ClusterMarker(即Rectangle)和PromotionMarker (即Image)。 I would like to assign them to MapQuickItem (which is delegate for MapItemView) using sourceItem property.
我想使用sourceItem属性将它们分配给MapQuickItem(这是MapItemView的委托)。
Here is how I'm doing it: 这是我的做法:
MapItemView
{
id: promMarkersView
...
delegate: MapQuickItem
{
id: promMarkersDelegate
coordinate: QtPositioning.coordinate(lat, lon)
sourceItem: cntOfChilds ? ClusterMarker {id: c} : PromotionMarker {id: p}
...
}
}
But now I'm getting two errors. 但是现在我遇到两个错误。 First is pointing to the first bracket of
{id: c}
: Expected token ':' , and the second one is pointing to the :
Unexpected token ':' 第一个指向
{id: c}
的第一个括号: 预期标记':' ,第二个指向:
意外的标记':'
What is the proper way to make this assignment? 进行此分配的正确方法是什么?
I'm not sure if this the best way, but is seems to work. 我不确定这是否是最好的方法,但似乎可行。
Create the item dynamically from components: 从组件动态创建项目:
...
sourceItem: index % 2 ? mapItemDelegate1.createObject() : mapItemDelegate2.createObject()
Component.onDestruction: sourceItem.destroy();
...
And specify your items as Components, for example: 并将您的项目指定为“组件”,例如:
Component {
id: mapItemDelegate1
Rectangle {
color: "red"
width: 6
height: 6
}
}
Component {
id: mapItemDelegate2
Rectangle {
color: "blue"
radius: 2
width: 6
height: 6
}
}
The best way is to use a Loader
: 最好的方法是使用
Loader
:
MapItemView {
id: promMarkersView
...
delegate: MapQuickItem {
id: promMarkersDelegate
coordinate: QtPositioning.coordinate(lat, lon)
sourceItem: Loader {
sourceComponent: cntOfChilds ? c : p
}
...
}
Component {
id: c
ClusterMarker {}
}
Component {
id: p
PromotionMarker {}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.