Skip to content Skip to sidebar Skip to footer

Refreshing QQuickWidget In QML File After Button Click

Created a MapWidget using QQuickWidget and a qml file to zoom into the given location coordinates. However, the map does not refresh whenever the coordinates changed. I am trying t

Solution 1:

Considering that you want to move only one marker you must create a QObject that has the position as q-properties, export the object and make a binding in QML.

main.py

import os
from PyQt5 import QtCore, QtGui, QtWidgets, QtQuickWidgets, QtPositioning

class MarkerObject(QtCore.QObject):
    coordinateChanged = QtCore.pyqtSignal(QtPositioning.QGeoCoordinate)
    sourceChanged = QtCore.pyqtSignal(QtCore.QUrl)

    def __init__(self, parent=None):
        super(MarkerObject, self).__init__(parent)
        self._coordinate = QtPositioning.QGeoCoordinate()
        self._source = QtCore.QUrl()

    def getCoordinate(self):
        return self._coordinate

    def setCoordinate(self, coordinate):
        if self._coordinate != coordinate:
            self._coordinate = coordinate
            self.coordinateChanged.emit(self._coordinate)

    def getSource(self):
        return self._source

    def setSource(self, source):
        if self._source != source:
            self._source = source
            self.sourceChanged.emit(self._source)

    coordinate = QtCore.pyqtProperty(QtPositioning.QGeoCoordinate, fget=getCoordinate, fset=setCoordinate, notify=coordinateChanged)
    source = QtCore.pyqtProperty(QtCore.QUrl, fget=getSource, fset=setSource, notify=sourceChanged)

class MapWidget(QtQuickWidgets.QQuickWidget):
    def __init__(self, parent=None):
        super(MapWidget, self).__init__(parent,
            resizeMode=QtQuickWidgets.QQuickWidget.SizeRootObjectToView)
        self._marker_object = MarkerObject(parent)
        self._marker_object.setSource(QtCore.QUrl("http://maps.gstatic.com/mapfiles/ridefinder-images/mm_20_red.png"))
        self.rootContext().setContextProperty("marker_object", self._marker_object)
        qml_path = os.path.join(os.path.dirname(__file__), "main.qml")
        self.setSource(QtCore.QUrl.fromLocalFile(qml_path))

    @QtCore.pyqtSlot(QtPositioning.QGeoCoordinate)
    def moveMarker(self, pos):
        self._marker_object.setCoordinate(pos)

class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)
        self._lat_spinbox = QtWidgets.QDoubleSpinBox(
            minimum=-90, 
            maximum=90, 
            decimals=6, 
            value=59.91
        )
        self._lng_spinbox = QtWidgets.QDoubleSpinBox(
            minimum=-180, 
            maximum=180, 
            decimals=6, 
            value=10.75
        )
        search_button = QtWidgets.QPushButton("Search", clicked=self.search)
        self._map_widget = MapWidget()
        flay = QtWidgets.QFormLayout(self)
        flay.addRow("Latitude:", self._lat_spinbox)
        flay.addRow("Longitude:", self._lng_spinbox)
        flay.addRow(search_button)
        flay.addRow(self._map_widget)
        self.search()

    @QtCore.pyqtSlot()
    def search(self):
        lat = self._lat_spinbox.value()
        lng = self._lng_spinbox.value()
        self._map_widget.moveMarker(QtPositioning.QGeoCoordinate(lat, lng))

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

main.qml

import QtQuick 2.9
import QtLocation 5.12
import QtPositioning 5.12

Rectangle {
    id:rectangle
    width: 640
    height: 480
    Plugin {
        id: osmPlugin
        name: "osm"
    }
    Map {
        id: map
        anchors.fill: parent
        plugin: osmPlugin
        center: marker_object.coordinate
        zoomLevel: 10

        MapQuickItem {
        coordinate: marker_object.coordinate
        anchorPoint.x: image.width
        anchorPoint.y: image.height
            sourceItem: Image {
                id: image
                source: marker_object.source
            }
        }
    }
}

enter image description here


Post a Comment for "Refreshing QQuickWidget In QML File After Button Click"