2

I'm trying that to create a class that inherits from QgsMapTool and deactivates itself after a canvasPressEvent is called. I've tried three options (in the code below) but none of them is working.

class getCoordinateTool (QgsMapTool):
def __init__(self,owner, canvas,onclick,projectInstance):
    """constructor"""
    super().__init__(canvas)
    self.owner = owner
    self.canvas = canvas
    self.onClick = onclick
    self.project_instance = projectInstance

def deactivate(self):
    #option 1:
    #super().deactivate()

    #option 2:
    #super(getCoordinateTool, self).deactivate()

    #option 3:
    QgsMapTool.deactivate(self)
    self.deactivated.emit()

def canvasPressEvent(self, e):
    super().canvasPressEvent(e)
    pos=self.toMapCoordinates(e.pos())
    crsSrc = QgsProject.instance().crs()
    (x,y) = coorTransform(pos, crsSrc,self.project_instance)
    self.onClick(x,y)
    #deactivate the select tool
    self.deactivate()

2 Answers 2

1

I am unable to test it right now, but here is a probably working version:

class getCoordinateTool (QgsMapTool):
    def __init__(self,owner, canvas,onclick,projectInstance):
        """constructor"""
        QgsMapTool.__init__(self, canvas)
        self.owner = owner
        self.canvas = canvas
        self.onClick = onclick
        self.project_instance = projectInstance

    def canvasPressEvent(self, e):
        QgsMapTool.canvasPressEvent(self, e)
        pos=self.toMapCoordinates(e.pos())
        crsSrc = QgsProject.instance().crs()
        (x,y) = coorTransform(pos, crsSrc,self.project_instance)
        self.onClick(x,y)
        #deactivate the select tool
        self.deactivate()

You do not need to override the deactivate method from QgsMapTool as you are not adding any new functionality to it(the "deactivated" signal is emitted automatically when you call deactivate() from QgsMapTool in the last line of the code). If you need to override it, do it like this:

def deactivate(self):
    # Do whatever you want here
    QgsMapTool.deactivate(self)

This is because any code after QgsMapTool.deactivate() probably won't be executed. And I can't recommend using super() on QgsMapTool as it may not be a new style class

1
  • 3
    This doesn't seem to work for me, but adding the line self.canvas.unsetMapTool(self) as the last line of canvasPressEvent(self, e): does work! Feb 21, 2018 at 8:55
0

To summarize this worked for me.

to set a MapTool do:

class MyTool(QgsMapToolEmitPoint)
    def __init__(self, canvas):
        self.canvas = canvas
        QgsMapToolEmitPoint.__init__(self, self.canvas)

    def your_tool_methods(self):
        pass


canvas = iface.mapCanvas()
my_tool = MyTool(canvas)  # should create a QgsMapToolEmitPoint instance
canvas.setMapTool(my_tool)

and to disable the tool do:

canvas.unsetMapTool(my_tool)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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