Good day. I create a program for working with maps. The functional is still not quite simple and is being developed in Qt C ++. For the first display of the application, I use QQuickWidget to display the map. I liked QML very much, but I still struggle with the fact that I don’t fully understand how the interaction with js works, or rather the work with qml types.

I suggest to look at an example. The task is to put a few gps tracks on the map. Tracks are presented in GPX format (this is an ordinary xml. Example ). For the sake of experiment, I display them in a ListView.

Rectangle { id: rectmp width: 300 height: 300 XmlListModel { id: xmlModel source: "qrc:/gpx.gpx" query: "/gpx/trk/trkseg/trkpt" XmlRole { name: "lat"; query: "@lat/string()"} XmlRole { name: "lon"; query: "@lon/string()"} } ListView { width: 200; height: 300 model: xmlModel delegate: Text { text: "lat:" + lat + " lon:" + lon} } } 

At the output I get a list of coordinates. This is nice, but I can't figure out how elegantly to transfer this data to, for example, MapPolyLine?

With raw data in the documentation, it looks like this:

 MapPolyline { id: track1 line.width: 3 line.color: 'green' path: [ { latitude: -27, longitude: 153.0 }, { latitude: -27, longitude: 154.1 }, { latitude: -28, longitude: 153.5 }, { latitude: -29, longitude: 153.5 } ] } 

where {latitude: -27, longitude: 153.0} is the type of coordinate. And how to bring in lat and lon values ​​from xml I can’t figure it out.

With all this, in my case the number of tracks is different. How beautiful to make several PolyLine'ov and be able to continue to work with each of them? Again, each track involves working with a mouse, etc. Hence another question: how, similarly to C and JS, to make several objects of the same class in QML?

    1 answer 1

    Too many questions in one, do not need to do so. To get the path for the polyline from the XML model, you can use code like this:

     XmlListModel { id: xmlModel source: "qrc:/gpx.gpx" query: "/gpx/trk/trkseg/trkpt" XmlRole { name: "lat"; query: "@lat/string()"} XmlRole { name: "lon"; query: "@lon/string()"} onStatusChanged: { if (status == XmlListModel.Ready) { var points = []; for(var i=0; i < count; i++) { var item = get(i); points.push({latitude: item.lat, longitude: item.lon}); } track1.path = points; } } } 

    I did not check it, but it should work. To work with the mouse in QML there is a MouseArea . Component creation is described in Dynamic QML Object Creation from JavaScript , regular JS classes can be created via new , but you are unlikely to need it.

    • got you Thank you for the clarification. Your code is working! Only it was not obvious that this is possible. Looks like a matter of experience. - Vladislav Buzdin
    • About the fact that the creation of components is not useful. Then how, for example, display multiple tracks with beautiful code? Maybe 2, and maybe 10. Do not prescribe the same XmlListModel for each. There is an idea to create a ListView with paths, and then delegate the display of the XmlListModel - Vladislav Buzdin
    • @VladislavBuzdin, I talked about JS classes, components sometimes have to be created. As for how to do it right: everything comes with experience. Study the examples, see what yes how - study the documentation. Personally, I prefer to build the whole model in C ++, and in QML I’ll deal exclusively with the display of the model. And, by the way, if my answer helped you - check it with a daw (next to the answer), it will accept it as an answer to the question. - ixSci