Qt 如何动态绘制多边形并使其点/标记在QML中可移动?
我遇到了一个问题,我必须使用鼠标在QML地图上动态绘制一个多边形,并使其点可移动,以便用户可以更改这些点的位置。有一个非常好的例子,它帮助我至少动态添加一些点/标记,并通过线将它们连接起来,但它不允许标记移动Qt 如何动态绘制多边形并使其点/标记在QML中可移动?,qt,qml,Qt,Qml,我遇到了一个问题,我必须使用鼠标在QML地图上动态绘制一个多边形,并使其点可移动,以便用户可以更改这些点的位置。有一个非常好的例子,它帮助我至少动态添加一些点/标记,并通过线将它们连接起来,但它不允许标记移动 有人能在这方面帮助我吗?在下面的代码中,右键单击将添加一个标记,您可以右键单击拖动一个标记 添加的逻辑很简单,即检测鼠标右键单击,并通过将其添加到与处理标记和MapPolygon点的MapItemView关联的模型中来获取位置信息 另一方面,拖动的逻辑是首先在没有按下标记的情况下进行检测,
有人能在这方面帮助我吗?在下面的代码中,右键单击将添加一个标记,您可以右键单击拖动一个标记 添加的逻辑很简单,即检测鼠标右键单击,并通过将其添加到与处理标记和MapPolygon点的MapItemView关联的模型中来获取位置信息 另一方面,拖动的逻辑是首先在没有按下标记的情况下进行检测,以便使用附加到每个标记的鼠标ea获取该元素的索引,从而禁用地图的“手势”。标记的MouseArea经过配置,以便它们继续将鼠标事件传播到其他元素,因为必须在地图上进行释放检测,为此,使用位置更改和释放信号更新标记的位置,并在必要时恢复变量
import QtQuick 2.14
import QtQuick.Window 2.14
import QtLocation 5.14
import QtPositioning 5.14
Window {
visible: true
width: 640
height: 480
property int currentIndex: -1
ListModel{
id: polygonmodel
}
Map {
id: map
anchors.fill: parent
plugin: Plugin {
name: "osm"
}
gesture.enabled: currentIndex == -1
center: QtPositioning.coordinate(59.91, 10.75) // Oslo
zoomLevel: 14
MapItemView{
z: polygon.z + 1
model: polygonmodel
delegate: MapQuickItem{
anchorPoint: Qt.point(sourceItem.width/2, sourceItem.height/2)
coordinate: QtPositioning.coordinate(model.coords.latitude, model.coords.longitude)
sourceItem: Image {
width: 40
height: 40
source: "http://maps.gstatic.com/mapfiles/ridefinder-images/mm_20_red.png"
MouseArea{
anchors.fill: parent
acceptedButtons: Qt.LeftButton
propagateComposedEvents: true
onPressed: {
currentIndex = index
mouse.accepted = false
}
}
}
}
}
MapPolygon{
id: polygon
border.color: "green"
border.width: 10
}
MouseArea{
anchors.fill: parent
acceptedButtons: Qt.LeftButton | Qt.RightButton
onClicked: {
var point = Qt.point(mouse.x, mouse.y)
var coord = map.toCoordinate(point);
if (mouse.button == Qt.RightButton)
addMarker(coord)
}
onPositionChanged: {
if (currentIndex != -1){
var point = Qt.point(mouse.x, mouse.y)
var coord = map.toCoordinate(point);
if(coord.isValid)
moveMarker(currentIndex, coord)
}
}
onReleased: {
if (mouse.button == Qt.LeftButton && currentIndex != -1){
var point = Qt.point(mouse.x, mouse.y)
var coord = map.toCoordinate(point);
if(coord.isValid)
moveMarker(currentIndex, coord)
currentIndex = -1;
}
}
}
}
function moveMarker(index, coordinate){
polygonmodel.set(index, {"coords": coordinate})
var path = polygon.path;
path[index] = coordinate
polygon.path = path
}
function addMarker(coordinate){
polygonmodel.append({"coords": coordinate})
polygon.addCoordinate(coordinate)
}
}
1) 我的解决方案不实现多边形,而是实现一组连续线,因此我怀疑您想要多边形还是只需要连续线?2) 我的解决方案使用标记,但在您对问题的描述中,您没有指出任何标记。你需要标记吗?1)我想要一个多边形2)我需要一个可移动的标记。@eyllanesc你对以下问题的回答有点帮助,但仍然没有满足我的要求