Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set QML MapPolyline Path Property

Tags:

qt

qml

qtquick2

I have a MapPolyline object in my QtQuick Project. I need to change the path property from the C++ file, but I have no idea how to do that.

My qml - File:

MapPolyline {
    id: mapline
    objectName: "MapLine"
    line.width: 5
    line.color: "green"
    path:
    {[
        { latitude: 47.219791, longitude: 9.546032 },
        { latitude: 47.219657, longitude: 9.542508 },
        { latitude: 47.2194446, longitude: 9.5437876 }
    ]}
}

And now I want to change the content of the path property from the C++ file.

Thanks for the help!

like image 607
Martin Bischof Avatar asked Oct 20 '22 02:10

Martin Bischof


1 Answers

Qt Location works okay from c++. You don't need to mess with any QJSValue stuff. Create a property in your QObject and return a QVariantList. Fill the QVariantList with QGeoCoordinate (the points in your line). In QML set the MapPolyline's path to the QObject QVariantList property. QML will automatically convert the QVariantList into a Javascript Array. QGeoCoordinates are qml coordinate type so the conversion is transparent. Any time you see a "list" type in qml, you can always return QVariantList from c++ or QVariantMap if you want to populate a Javascript Object (or create a Q_GADGET).

One thing that got me was that my QGeoCoordinates returned from c++ wasn't valid. If the QGeoCoordinates are invalid, the MapPolyLine fail to populate and will create a message: "Unsupported path type". Make sure QGeoCoordinate isValid() before adding it to the QVariantList.

like image 156
vpicaver Avatar answered Dec 28 '22 06:12

vpicaver