Leonid
Leonid

Reputation: 93

How to assign custom object to sourceItem?

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?

Upvotes: 0

Views: 235

Answers (2)

augre
augre

Reputation: 2051

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 {}
    }
}

Upvotes: 0

onion
onion

Reputation: 437

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
    }
}

Upvotes: 0

Related Questions