3

I have a set of QPointF in MarkerModel which subclasses from AbstractListModel. Each such marker have a status, depending on which they are colored. I want to draw all these markers on the map along with a polyline that connects all the points that have a specific status. And I will update the model from C++ side. This is my QML

Map {
    id: map
    anchors.fill: parent
    plugin: mapPlugin
    center: QtPositioning.coordinate(22.5726, 88.3639)
    zoomLevel: 14

    MapItemView {
        model: markerModel
        // delegate: markerDelegate // markerDelegate works
        delegate: routeDelegate // routeDelegate does not work
    }

    Component {
        id: markerDelegate

        MapQuickItem{
            anchorPoint: Qt.point(2.5, 2.5)
            coordinate: QtPositioning.coordinate(position.x, position.y)
            zoomLevel: 0
            sourceItem: Rectangle{
                width:  settings.marker_size;
                height: settings.marker_size;
                radius: settings.marker_size/2;
                color:  settings.marker_colors[status]
                border.color: "white"
                border.width: 1
            }
        }
    }
    Component{
        id: routeDelegate

        MapRoute{
            route: markerModel
            line.color: "blue"
            line.width: 5
            smooth: true
            opacity: 0.8
        }
    }
}

I actually want both, the points and the polyline on the scene. However as I don't know how to put both of them in the scene I was first trying to show the points from the model using markerDelegate, which worked. Now I want to view these points as a polyline with routeDelegate. But it complains

Unable to assign MarkerModel to QDeclarativeGeoRoute

Neel Basu
  • 12,638
  • 12
  • 82
  • 146

1 Answers1

0

If you source MapRoute from a RouteModel through a MapItemView, you always assign routeData to route. routeData is the role that RouteModel exposes, to let you access the Route elements.

Now, for your specific case, it seems that MapRoute is unsuitable. The best approach seems to me to have 2 separate models: one exposing one js array per row, that you assign to the path property of a MapPolyline delegate, and one exposing one QGeoCoordinate per row (so many more rows), that you will use with a MapCircle or a MapQuickItem delegate

Pa_
  • 641
  • 1
  • 5
  • 17
  • Sorry I didn't understand, `markerModel` is actually subclass of `AbstractListModel`. I don't know how to create a `RouteModel` in QT (C++) – Neel Basu Sep 05 '17 at 14:41
  • Ok, sorry, i got confused from the example, maybe you can fix it keeping only the delegate that you use? In any case if you want to use a MapRoute with a model that is not a RouteModel, you have to have to define a role in your model through which you provide Route objects (QDeclarativeGeoRoute). Edit: actually, now that i read your question again, you cant use QPointF to create a MapRoute or a MapPolyline. You first have to convert them to QGeoCoordinates. – Pa_ Sep 05 '17 at 15:48
  • How can I create QDeclarativeGeoRoute objects from C++ ? Where is that class ? – Neel Basu Sep 06 '17 at 19:09
  • in QML that's a "Route". Then again, filling a Route by hand is not really a use case. Considering that you aren't doing routing, but you are sourcing your data from a model, you should considering using a MapPolyline – Pa_ Sep 07 '17 at 11:51
  • I am solving it by MapPolyline, but how can I create a QGeoRoute from C++ side and let it be used in QML ? – Neel Basu Sep 11 '17 at 13:25
  • Do you need a QGeoRoute? QGeoRoute is supposed to be an object containing directions, maneuvers, and everything else that would be necessary to do navigation. Is your item a route for which you have all this? If not, the answer is, don't do it, as you simply need a list of coordinates. If yes, otoh, the probably only way is by implementing a plugin that does routing. – Pa_ Sep 11 '17 at 13:54