- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个小问题让我花费了上午的大部分时间。我正在尝试根据 pyQt 使用 Geocoder 传入的本地坐标在 QML 中绘制 map 屏幕。我可以绘制 map OK,但一旦发出信号,它似乎就不会更新。我对 QML 和信号/插槽比较陌生。任何帮助都表示赞赏。
主.py
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQml import QQmlApplicationEngine
from PyQt5.QtCore import QObject, pyqtSignal, pyqtSlot
class Point(QObject):
def __init__(self, lat, lng):
QObject.__init__(self)
self.lat = lat
self.lng = lng
#Signal sending point
#Give the name of the argument through arguments=['lat', 'lng']
#Otherwise, it will not be possible to get it in QML
showCenter = pyqtSignal(float, float, arguments=['lat', 'lng'])
#Slot for emitting point
@pyqtSlot()
def emitPoint(self):
self.showCenter.emit(self.lat, self.lng)
if __name__ == "__main__":
import sys
import geocoder
#Create an instance of the application
app = QGuiApplication(sys.argv)
#Create a QML engine
engine = QQmlApplicationEngine()
#Create a Point object
g = geocoder.ip('me')
center_point = Point(g.latlng[0], g.latlng[1])
#And register it in the context of QML
engine.rootContext().setContextProperty("center_point", center_point)
#Load the qml file into the engine
engine.load("main.qml")
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: QtPositioning.coordinate(0.0, 0.0)
zoomLevel: 6
MapCircle {
id: dot
center: QtPositioning.coordinate(0.0, 0.0)
radius: 50
color: 'red'
}
Connections {
target: center_point
onEmitPoint: {
console.info("Point changed")
map.center = QtPositioning.coordinate(lat, lng)
dot.center = QtPositioning.coordinate(lat, lng)
}
}
}
Component.onCompleted: {
console.info("Signal Emitted")
center_point.emitPoint()
}
}
最佳答案
当你使用 Connections 时,如果目标信号被称为 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)
}
}
另一种选择是创建一个可以在 QML 中轻松使用的 qproperty,就像您在 another answer 中使用它一样。 :
主.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'
}
}
}
关于python - QML AppWindow 在信号发出后不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55026336/
我有一个小问题让我花费了上午的大部分时间。我正在尝试根据 pyQt 使用 Geocoder 传入的本地坐标在 QML 中绘制 map 屏幕。我可以绘制 map OK,但一旦发出信号,它似乎就不会更新。
当用户在WinUI3 Windows App SDK应用程序中调整主窗口大小时,我希望强制窗口保持16:9的纵横比。我尝试在SizeChanged事件处理程序中完成此操作...
当用户在WinUI 3 Windows App SDK应用程序中调整主窗口大小时,我希望强制窗口保持16:9的宽高比。我试图在SizeChanged事件处理程序中完成它.
当用户在WinUI3 Windows App SDK应用程序中调整主窗口大小时,我希望强制窗口保持16:9的纵横比。我尝试在SizeChanged事件处理程序中完成此操作...
我是一名优秀的程序员,十分优秀!