【发布时间】:2019-07-13 05:34:24
【问题描述】:
使用 QQuickWidget 和 qml 文件创建了一个 MapWidget 以放大给定的位置坐标。但是,只要坐标发生变化,地图就不会刷新。我正在尝试连接一个可以单击以更新地图的按钮,但到目前为止还没有运气。有没有办法获取按钮的 id 并将其传递给 qml 文件以在坐标更改时更新或刷新地图?
main.py
class MapWidget(QtQuickWidgets.QQuickWidget):
def __init__(self, parent=None):
super(MapWidget, self).__init__(parent,
resizeMode=QtQuickWidgets.QQuickWidget.SizeRootObjectToView)
model = MarkerModel(self)
self.rootContext().setContextProperty("markermodel", model)
self.rootContext().setContextProperty("lataddr", globals.latitude)
self.rootContext().setContextProperty("lonaddr", globals.longitude)
qml_path = os.path.join(os.path.dirname(__file__), "main.qml")
self.setSource(QtCore.QUrl.fromLocalFile(qml_path))
positions = [(globals.latitude, globals.longitude)]
urls = ["http://maps.gstatic.com/mapfiles/ridefinder-images/mm_20_red.png"]
for c, u in zip(positions, urls):
coord = QtPositioning.QGeoCoordinate(*c)
source = QtCore.QUrl(u)
model.appendMarker({"position": coord , "source": source})
class project_ui(QWidget):
def setup(self, window):
"omitted code"
self.btn = QPushButton('Search', self)
self.btn.setFixedWidth(200)
self.btn.clicked.connect("reload map in qml file")
main.qml
Rectangle {
id:rectangle
width: 640
height: 480
Plugin {
id: osmPlugin
name: "osm"
}
property variant locationTC: QtPositioning.coordinate(lataddr, lonaddr)
Map {
id: map
anchors.fill: parent
plugin: osmPlugin
center: locationTC
zoomLevel: 10
}
MapItemView{
model: markermodel
delegate: MapQuickItem {
coordinate: position_marker
anchorPoint.x: image.width
anchorPoint.y: image.height
sourceItem:
Image { id: image; source: source_marker }
}
}
}
【问题讨论】:
-
一些反馈??
-
@eyllanesc 非常感谢您的回复!按照您的示例,我已经能够使用 pyqtSlot 连接 main.py 和 main.qml 中的搜索按钮。我目前使用的是 QlineEdit 而不是 QDoubleSpinBox。一切正常!谢谢
标签: python python-3.x pyqt qml pyqt5