0

A would like to visualize MapQuickItem depend on if condition.

I have two custom objects ClusterMarker which is a Rectangle and PromotionMarker which is an Image object. I would like to assign them to MapQuickItem (which is delegate for MapItemView) using sourceItem property.

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 ':'

What is the proper way to make this assignment?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Leonid
  • 73
  • 1
  • 6
  • Possible duplicate of [Conditionally include component based on property value](https://stackoverflow.com/questions/27695717/conditionally-include-component-based-on-property-value) – eyllanesc Jan 05 '19 at 03:42

2 Answers2

0

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
    }
}
onion
  • 417
  • 3
  • 3
0

The best way is to use a 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 {}
    }
}
augre
  • 2,011
  • 12
  • 18