Instantiator, and dynamically created components

84 views Asked by At

I have a problem with the QGroundMap overriding. I have a custom QGround widget and I need to draw multiple MapPolyline objects on a QGroundMap dynamically, but the only way to do that is to inject it directly by using this code.

Component.onCompleted: {
    objMgr.createObjects([mapLine], _root.mapControl, true /* parentObjectIsMap */)
}

Standard MapItemView and other elements are not working properly.

I am trying to instantiate multiple MapPolyLine objects, but it`s not working properly either:

Instantiator {
    id: testInstantiator
    model: globalCoordinates

    function getIndex(index) {
        console.log("currentIndexIs")
        console.log(index)
        return index
    }

    delegate: Component {
        id: mapLine

        MapPolyline {
            id: targetPolyLine
            line.width: 3
            line.color: "red"
            z: QGroundControl.zOrderWaypointLines
            path: testInstantiator.model[index]
        }
    }
}

And my globalCoordinates model looks like this:

property var globalCoordinates: [
        [QtPositioning.coordinate(42, 13), QtPositioning.coordinate(42, 18)],
        [QtPositioning.coordinate(42, 13), QtPositioning.coordinate(42, 20)],
        [QtPositioning.coordinate(42, 13), QtPositioning.coordinate(42, 22)],
        [QtPositioning.coordinate(42, 13), QtPositioning.coordinate(42, 24)],
        [QtPositioning.coordinate(42, 13), QtPositioning.coordinate(42, 26)]
    ]

The only way to draw a line inside the instantiator is to replace index with a number, like this:

Instantiator {
    id: testInstantiator
    model: globalCoordinates

    function getIndex(index) {
        console.log("currentIndexIs")
        console.log(index)
        return index
    }

    delegate: Component {
        id: mapLine

        MapPolyline {
            id: targetPolyLine
            line.width: 3
            line.color: "red"
            z: QGroundControl.zOrderWaypointLines
            path: testInstantiator.model[1] //it will draw a single line
        }
    }
}

I suspect, that Instantiator can not delegate Component more than one time. Or it cannot transfer properly all necessary data.

Are there any ways to inject MapPolyLine to a QGroundMap, or delegate the injected Component properly?

1

There are 1 answers

1
Stephen Quan On

You can use MapItemView. If your model was a simple array, we can set the model to length - 1. Then we just use index and index + 1 to construct the MapPoyline

    property var globalCoordinates: [
        { latitude: -27, longitude: 153.0 },
        { latitude: -27, longitude: 154.1 },
        { latitude: -28, longitude: 153.5 },
        { latitude: -29, longitude: 153.5 }
    ]

    Map {
       id: map
       anchors.fill: parent
       plugin: mapPlugin
       zoomLevel: 14

       MapItemView {
           model: globalCoordinates.length - 1
           delegate: MapPolyline {
              line.width: 3
              line.color: 'red'
              path: [
                  globalCoordinates[index],
                  globalCoordinates[index + 1]
              ]
          }
       }
    }