Qt World Summit: Register Today!

Can't set MapPolyline path from C++

  • Hello, I'm trying to set a geopath for a QML MapPolyline in a QML map from C++. No matter what I try: it does not show the path. I'm trying to use QMetaObject::invokeMethod or setProperty method:

    QQuickWidget mapWidget(QUrl("qrc:/main.qml"));
    QObject *trackPolyObject = mapWidget.rootObject()->findChild<QObject*>("trackPolyLine");
        int retVal;
        QMetaObject::invokeMethod(trackPolyObject, "setPath", Qt::DirectConnection,
                                  Q_RETURN_ARG(int, retVal), Q_ARG( QGeoPath, trackpointsmodel.currentGeoPath()));
        //bool ret = trackPolyObject->setProperty("path", trackpointsmodel.path());
        qDebug() << retVal;

    QML side:

            MapPolyline {
                id: trackPolyLine
                objectName: "trackPolyLine"
                line.width: 4
                line.color: "darkBlue"
                antialiasing: true

    The "trackpointsmodel" returns valid data in my application.
    Any hint? The QML part works and shows the route when I set the path property with some data in QML.

  • Found the cause of the problem: The doc of the MapPolyline QML class has an error. The "setPath" method has "void" return type and NOT int as described in the documentation. Thus the invoke methid call should look like this:

    QMetaObject::invokeMethod(trackPolyObject, "setPath", Q_ARG( QGeoPath, path));

  • @rykomick It is not recommended that you access QML objects directly from C++, it is best to create a QObject that has a property of the same type that the method requires and export it to QML through setContextProperty() or another mechanism and make the connection in QML.

Log in to reply