2017-07-06 52 views
0

大家好!QML:如何从C++代码正确传递属性到PluginParameter值?

我有通过C++QML传递属性值的问题。我的项目是桌面应用程序,必须与Windows下的地图一起使用。所以,在阅读文档后,我通过QML使用Qt Location找到了最佳解决方案。我选择了OSM Plugin

一切正常,但我需要手动定位缓存到自定义目录。所以为此,我想将C++代码中的此类属性(cachePath)值传递给QML。的C++代码

部分:QML代码

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

重要组成部分:

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 ...> 
} 

所以调试说,一切都好和财产被传递。但是在使用此自定义目录中的地图后没有新的贴图。

但是,如果我手动输入value: "C:/111/" - 一切工作正常,目录补充新的缓存切片。

可能是什么问题?

感谢您的提前!

+0

你有任何错误/警告消息控制台?如果我用C++改变插件参数运行类似的例子,我会得到以下消息:'QML Map:Plugin是一次写入属性,不能再次设置' – folibis

+0

@folibis,no我在控制台中没有这样的消息。为什么我应该?我首先设置属性,然后才加载QML。对于这个插件参数,它必须是第一个和最后一个值。 – someoneinthebox

+0

它似乎会在5.9.2中修复 –

回答

0

如果有人有兴趣就可以解决这样的问题:

C++方面:

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侧:

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 <...> } 

<...> 

} 
0

你已经尝试过这样的事情:

在C++

QObject *objYourObject; 
objYourObject = rootObject->findChild<QObject *>("pluginName"); 
objYourObject->setProperty("cachePath", "C:/111/"); 

在您的QML文件:

Rectangle { 
    objectName: "pluginName" 

    property string cachePath: "" 

    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 } 
     } 
    } 
} 

对我来说,它的工作原理和我使用它,每当我需要它。

我希望它有帮助。

+0

我不能这样做,因为@folibis说:'QML地图:插件是一次写入属性,不能再次设置。 – someoneinthebox