1

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?

3
  • 1
    You forgot to add self as the first argument of your onMapClick() method. It should be: def onMapClick(self, point):. The class instance object needs to be the first argument of all class methods.
    – Ben W
    Nov 19, 2021 at 9:51
  • 2
    I'm so blind. Thank you. Put your text in an answer, then I can accept it ;) Nov 19, 2021 at 9:55
  • No problem, done :-)
    – Ben W
    Nov 19, 2021 at 10:10

1 Answer 1

3

You just forgot to add self as the first argument of your onMapClick() method. The class instance object needs to be the first argument of all class methods. The canvasClicked() signal also emits the Qt.MouseButton object which caused the event which you can also catch.

Your slot method definition should look like:

def onMapClick(self, point, button):
    logging.warning('onMapClick: '+point.asWkt())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.