Когда вы используете Соединения, если целевой сигнал называется name_of_signal
, тогда вы должны использовать onName_of_signa
l, в вашем случае сигнал showCenter
, поэтому вы должны использовать onShowCenter
:
Connections {
target: center_point
onShowCenter: { // <---
console.info("Point changed")
map.center = QtPositioning.coordinate(lat, lng)
dot.center = QtPositioning.coordinate(lat, lng)
}
}
Другой вариант - создать свойство q, которое можно легко использовать в QML при его использовании в другой ответ :
main.py
from PyQt5 import QtCore, QtGui, QtPositioning, QtQml
class PointObject(QtCore.QObject):
coordinateChanged = QtCore.pyqtSignal(QtPositioning.QGeoCoordinate)
def __init__(self, parent=None):
super(PointObject, self).__init__(parent)
self._coordinate = QtPositioning.QGeoCoordinate()
def getCoordinate(self):
return self._coordinate
def setCoordinate(self, coordinate):
if self._coordinate != coordinate:
self._coordinate = coordinate
self.coordinateChanged.emit(self._coordinate)
coordinate = QtCore.pyqtProperty(QtPositioning.QGeoCoordinate, fget=getCoordinate, fset=setCoordinate, notify=coordinateChanged)
if __name__ == "__main__":
import os
import sys
import geocoder
#Create an instance of the application
app = QtGui.QGuiApplication(sys.argv)
#Create a QML engine
engine = QtQml.QQmlApplicationEngine()
#Create a Point object
g = geocoder.ip('me')
center_point = PointObject()
center_point.setCoordinate(QtPositioning.QGeoCoordinate(*g.latlng))
#And register it in the context of QML
engine.rootContext().setContextProperty("center_point", center_point)
#Load the qml file into the engine
qml_path = os.path.join(os.path.dirname(__file__), "main.qml")
engine.load(QtCore.QUrl.fromLocalFile(qml_path))
if not engine.rootObjects():
sys.exit(-1)
engine.quit.connect(app.quit)
sys.exit(app.exec_())
main.qml
import QtQuick 2.5
import QtQml 2.0
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.2
import QtPositioning 5.9
import QtLocation 5.9
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Map Point demo")
color: "whitesmoke"
Plugin {
id: mapPlugin
name: "esri" //"mapboxgl" "osm" "esri"
}
Map {
id: map
anchors.fill: parent
plugin: mapPlugin
center: center_point.coordinate
zoomLevel: 6
MapCircle {
id: dot
center: center_point.coordinate
radius: 50
color: 'red'
}
}
}