For QGIS I have a plugin named GGAImporter:
from .point_tool import PointTool
class GGAImporter:
...
def addFachdatum(self):
self.mapTool = PointTool(self.iface.mapCanvas())
self.beforeMapTool=self.iface.mapCanvas().mapTool()
self.iface.mapCanvas().setMapTool(self.mapTool)
self.mapTool.canvasClicked.connect(self.onMapClick)
def onMapClick(point):
logging.warning('onMapClick: '+point.asWkt())
In a several file point_tool.py there is the class for the tool:
from qgis.gui import QgsMapTool, QgsMapToolEmitPoint
from qgis.core import Qgis, QgsMessageLog
from PyQt5.QtCore import pyqtSignal
class PointTool(QgsMapToolEmitPoint):
canvasClicked = pyqtSignal('QgsPointXY')
def __init__(self, canvas):
super(QgsMapTool, self).__init__(canvas)
def canvasReleaseEvent(self, event):
point_canvas_crs = event.mapPoint()
self.canvasClicked.emit(point_canvas_crs)
QgsMessageLog.logMessage('canvasReleaseEvent '+point_canvas_crs.asWkt())
The logmessage from point_tool is correctly:
2021-11-19T09:51:29 WARNING canvasReleaseEvent POINT(646484.461160849314183 5857199.37299698870629072)
But in GGAImporter-class I get an error:
2021-11-19T09:51:29 WARNING Traceback (most recent call last):
File "C:/Users/sschmidt/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\gga_importer\gga_importer.py", line 427, in onMapClick
logging.warning('onMapClick: '+point.asWkt())
AttributeError: 'GGAImporter' object has no attribute 'asWkt'
It seems that the parameter of type QgsPointXY by emitting to slot get lost and is then my main-class of the plugin. Is there an error with PyQt5.QtCore.pyqtSignal?
self
as the first argument of youronMapClick()
method. It should be:def onMapClick(self, point):
. The class instance object needs to be the first argument of all class methods.