1

Hello everyone!

I've got the problem with passing property value through C++ to QML. My project is desktop application that must works with maps under Windows. So, after reading docs I found the best solution via QML using Qt Location. I chose OSM Plugin.

Everything works good but I need to manually locate cache into custom directory. So for that I want to pass such property (cachePath) value from C++ code to QML.

Part of C++ code:

QQuickView *view = new QQuickView;
view->rootContext()->setContextProperty("cachePath", "C:/111/");
view->setSource(QUrl(QStringLiteral("qrc:/qml/OSMView.qml")));

Important part of QML code:

Map
{
    zoomLevel: 10

    plugin: Plugin
    {
        name: "osm"
        PluginParameter { name: "osm.mapping.highdpi_tiles"; value: true }
        PluginParameter { name: "osm.mapping.offline.directory"; value: cachePath }
        PluginParameter { name: "osm.mapping.cache.directory"; value: cachePath }
    }

    <... nevermind ...>
}

So debug said that everything alright and property is passed. But there is no new tiles after work with maps in this custom directory.

But, if I type manually value: "C:/111/" - everything works fine and directory is replenished with new cache tiles.

What could be the problem?

Thanks for advance!

Shtol Krakov
  • 1,260
  • 9
  • 20
  • Do you have any error/warning message in the console? If I run similar example with changing plugin parameters from C++ I get message: `QML Map: Plugin is a write-once property, and cannot be set again` – folibis Jul 06 '17 at 10:58
  • @folibis, no I have no such message in the console. And why I should? I firstly set property and only then - load QML. For this plugin parameter it must be the first and the final value. – Shtol Krakov Jul 06 '17 at 11:12
  • it seems like it's gonna be fixed in 5.9.2 – Pa_ Sep 13 '17 at 15:51
  • Can you cite a bug tracker number for that Paul? – Richard Lang Dec 21 '17 at 04:46

1 Answers1

3

If someone interesting you can solve problem like this:

C++ side:

QVariantMap params
{
    {"osm.mapping.highdpi_tiles", YOUR_CUSTOM_VALUE},
    {"osm.mapping.offline.directory", YOUR_CUSTOM_VALUE},
    {"osm.mapping.cache.directory", YOUR_CUSTOM_VALUE}
};

QQuickView *view = new QQuickView;
view->setSource(QUrl(QStringLiteral("qrc:/qml/OSMView.qml")));
QObject *item = (QObject *) view->rootObject();
QMetaObject::invokeMethod(item, "initializePlugin", Q_ARG(QVariant, QVariant::fromValue(params)));

QML side:

Item
{
    id: osmMain    
    property variant parameters

    function initializePlugin(pluginParameters)
    {
        var parameters = new Array;
        for(var prop in pluginParameters)
        {
            var parameter = Qt.createQmlObject('import QtLocation 5.6; PluginParameter{ name: "'+ prop + '"; value: "' + pluginParameters[prop] + '"}', map)
            parameters.push(parameter)
        }
        osmMain.parameters = parameters
        map.plugin = Qt.createQmlObject('import QtLocation 5.6; Plugin{ name: "osm"; parameters: osmMain.parameters }', osmMain)
    }

    Map { id: map <...> }

<...>

}
Shtol Krakov
  • 1,260
  • 9
  • 20