Qt 在QML映射中动态更改osm插件的自定义主机URL

Qt 在QML映射中动态更改osm插件的自定义主机URL,qt,dictionary,plugins,qml,host,Qt,Dictionary,Plugins,Qml,Host,我有一个自定义图表主机,在目录结构中有几个平铺贴图: http://host/New_York/ http://host/Washington/ http://host/Montreal/ QML应用程序有一个组合框组件,允许用户选择要显示的图表 Map组件使用带有PluginParameter的osm插件,该插件指向用于图表的URL。我以为我可以简单地动态地给这个PluginParameter赋值,但它不起作用,即使赋值后该值也保持不变。我还尝试销毁插件对象,重新创建它并将其分配给Map对象

我有一个自定义图表主机,在目录结构中有几个平铺贴图:

http://host/New_York/
http://host/Washington/
http://host/Montreal/
QML应用程序有一个组合框组件,允许用户选择要显示的图表

Map组件使用带有PluginParameter的
osm
插件,该插件指向用于图表的URL。我以为我可以简单地动态地给这个PluginParameter赋值,但它不起作用,即使赋值后该值也保持不变。我还尝试销毁插件对象,重新创建它并将其分配给Map对象,但我得到一个错误,即
Plugin
属性为
只读

动态更改地图组件使用的插件对象的自定义主机URL的正确方法是什么

    Plugin {
        id: mapPlugin
        name: "osm"

        PluginParameter { id: charturl; name: "osm.mapping.custom.host"; }
    }

    Map {
        id: mapview
        plugin: mapPlugin
        activeMapType: supportedMapTypes[supportedMapTypes.length - 1]
...

    ComboBox {
...
        onCurrentIndexChanged: {
            charturl.value = cbItems.get(currentIndex).url
...

插件只能编写一次,因此以后无法更改,因此在此情况下,您必须使用Loader创建一个新地图:

main.qml

import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick.Layouts 1.14
import QtQuick.Controls 2.14

import QtLocation 5.14
import QtPositioning 5.14

Window {
    visible: true
    width: 640
    height: 480
    ColumnLayout {
        anchors.fill: parent
        ComboBox {
            id: combobox
            model: [
                "http://host/New_York/",
                "http://host/Washington/",
                "http://host/Montreal/"
            ]
            Layout.fillWidth: true
            onActivated: changeHost()
        }
        Loader{
            id: loader
            Layout.fillWidth: true
            Layout.fillHeight: true
            onStatusChanged: if (loader.status === Loader.Ready) console.log('Loaded')
        }
        Component.onCompleted: changeHost()
    }
    function changeHost(){
        var item = loader.item
        var zoomLevel = item ? item.zoomLevel: 14
        var center = item ? item.center: QtPositioning.coordinate(59.91, 10.75)

        loader.setSource("MapComponent.qml", {
                             "host": combobox.currentValue,
                             "center": center,
                             "zoomLevel": zoomLevel}
                         )
    }
}
import QtLocation 5.14

Map {
    id: map
    property string host: ""
    plugin: Plugin {
        name: "osm"
        PluginParameter {
            name: "osm.mapping.custom.host"
            value: map.host
        }
    }
    activeMapType: supportedMapTypes[supportedMapTypes.length - 1]
}
MapComponent.qml

import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick.Layouts 1.14
import QtQuick.Controls 2.14

import QtLocation 5.14
import QtPositioning 5.14

Window {
    visible: true
    width: 640
    height: 480
    ColumnLayout {
        anchors.fill: parent
        ComboBox {
            id: combobox
            model: [
                "http://host/New_York/",
                "http://host/Washington/",
                "http://host/Montreal/"
            ]
            Layout.fillWidth: true
            onActivated: changeHost()
        }
        Loader{
            id: loader
            Layout.fillWidth: true
            Layout.fillHeight: true
            onStatusChanged: if (loader.status === Loader.Ready) console.log('Loaded')
        }
        Component.onCompleted: changeHost()
    }
    function changeHost(){
        var item = loader.item
        var zoomLevel = item ? item.zoomLevel: 14
        var center = item ? item.center: QtPositioning.coordinate(59.91, 10.75)

        loader.setSource("MapComponent.qml", {
                             "host": combobox.currentValue,
                             "center": center,
                             "zoomLevel": zoomLevel}
                         )
    }
}
import QtLocation 5.14

Map {
    id: map
    property string host: ""
    plugin: Plugin {
        name: "osm"
        PluginParameter {
            name: "osm.mapping.custom.host"
            value: map.host
        }
    }
    activeMapType: supportedMapTypes[supportedMapTypes.length - 1]
}