QML将新的PathCurve元素追加到ShapePath中的List <pathelements>

时间:2019-03-22 12:48:49

标签: qt qml qt5

具体来说:我想用样条曲线连接地图上的多个点。可以通过鼠标单击添加新点,并且还应将其连接到现有路径。这些点存储在模型中,因此我也可以在C ++中访问它们。

不幸的是,我不知道如何将新的PathCurve元素追加到Shape :: ShapePath对象中的现有列表中。

我希望这样的事情应该起作用:

python2

我也尝试用C ++填充PathElements,但是PathCurve类似乎是私有的,只能在QML中使用。硬编码PathCurve Elements就像在每个在线示例中一样工作正常,但我想动态修改路径元素列表。

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:2)

您必须使用函数createQmlObject动态创建组件,但是为此必须牢记,它取决于您对MapQuickItem应用的zoomLevel的大小关系,因为这取决于绘画,因此PathCurve不使用坐标窗口,但形状的坐标,并根据其配置方式绘制形状。因此,在这种情况下,MapQuickItem的zoomLevel必须为0或与地图相同。考虑到上述情况,解决方案是:

import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Shapes 1.12
import QtPositioning 5.9
import QtLocation 5.3

Window {
    id: root
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    Plugin {
        id: mapPlugin
        name: "osm" // "mapboxgl" "osm" "esri"
    }

    Map {
        id: map
        anchors.fill: parent
        plugin: mapPlugin
        zoomLevel: 14
        center:  QtPositioning.coordinate(59.91, 10.75) // Oslo

        MapQuickItem {
            id: map_item
            coordinate: QtPositioning.coordinate(59.91, 10.75)
            anchorPoint.x : 0
            anchorPoint.y : 0
            zoomLevel: map.zoomLevel
            sourceItem: Shape {
                id: myShape
                anchors.fill: parent
                vendorExtensionsEnabled: false
                ShapePath {
                    id: myPath
                    strokeColor: "black"
                    strokeWidth: 2
                    capStyle: ShapePath.RoundCap
                    fillColor: "transparent"
                    startX: 0; startY: 0
                }
            }
        }
    }
    MouseArea {
        id: mousearea
        anchors.fill: map
        onClicked: {
            var item_pos = map.fromCoordinate(map_item.coordinate, false)
            var pathcurve = Qt.createQmlObject('import QtQuick 2.12; PathCurve {}',
                                               myPath);
            pathcurve.x = mouse.x - item_pos.x
            pathcurve.y = mouse.y - item_pos.y
            myPath.pathElements.push(pathcurve)
        }
    }
}

enter image description here

相关问题