我使用PyQt5通过UI文件中的QQuickWidget访问QML代码。我的QML文件创建地图并绘制点。我想从python代码中添加/修改这些点。我可以在python中访问QML中的Map对象,但是PyQt将它和MapQuickItem视为QQuickItems。我不知道如何在python中实际创建一个新的MapQuickItem并将其添加到Map对象中。我尝试使用必需的属性创建一个QQuickItem,然后使用addMapItem方法,但收到以下错误:
TypeError: unable to convert argument 0 of QQuickItem.addMapItem from 'QQuickItem' to 'QDeclarativeGeoMapItemBase*'"
我不知道如何在PyQt中创建一个QDeclarativeGeoMapItemBase
对象,也不知道是否应该用另一种方式来创建。在
如您所见,我在正确引用QML文件中的对象时也遇到了一些问题。self.map
或self.map.rootObject()
给我UI中的QQuickWidget,self.map.rootObject().children()[1]
给我QML中的Map对象。我更喜欢使用findChild()通过它们的ID来定位这些项,但一直未能找到。有更好的方法吗?是否应该创建一个Python对象来复制QML文件的结构?在
这是QML的一个示例。我将这个QML文件引用为UI文件中的QQuickWidget。在
Rectangle {
id:rectangle
Plugin {
id: osmPlugin
name: "osm"
}
property variant locationTC: QtPositioning.coordinate(44.951, -93.192)
Map {
id: map
anchors.fill: parent
plugin: osmPlugin
center: locationTC
zoomLevel: 10
MapQuickItem {
coordinate: QtPositioning.coordinate(44.97104,-93.46055)
anchorPoint.x: image.width * 0.5
anchorPoint.y: image.height
sourceItem:
Image { id: image; source: "marker.png" }
}
}
}
下面是PyQt代码的一个示例,我试图在其中创建MapQuickItem并将其添加到映射中。在
^{pr2}$我在Windows7上运行一切。PyQt5的开发是在Eclipse中使用PyDev和python3.4(32位)、Qt Creator 5.5中的QML编码和Qt Designer 5.5中的UI完成的。在
在本例中,我将创建一个存储数据的模型,通过setContextProperty()将其发送给QML,在QML端使用MapItemView和委托,这样您就可以有很多标记。在
主.py
主.qml
^{pr2}$相关问题 更多 >
编程相关推荐