Martin Bischof
Martin Bischof

Reputation: 115

How to set QML MapPolyline Path Property

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!

Upvotes: 6

Views: 2876

Answers (2)

vpicaver
vpicaver

Reputation: 1801

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.

Upvotes: 3

Mitch
Mitch

Reputation: 24396

I don't know much about Qt Location, but in general I think it's better to assume that if C++ access to a particular (QML) API was intended, there'd be a C++ API for it. I remember that this mailing list thread mentioned that there was a work-in-progress change that adds a C++ API.

That being said, if you're curious and think it'd be fun to try anyway, you should take a look at the QDeclarativePolylineMapItem::setPath() function, which calls parseCoordinate() to do the actual parsing of the path. It would probably involve using QObject::findObject() (or the property system) to obtain the MapPolyline object in C++, and then constructing a QJSValue (using the functions available in QJSEngine) that matches the expected format of setPath().

Upvotes: 0

Related Questions