qtqmlqquickitem

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?


Solution

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