3

I am writing a custom tool to get printmap coordinates from the canvas. The following code has been used to activate the map click on the canvas:

from qgis.gui import QgsMapToolEmitPoint

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

    def canvasPressEvent( self, e ):
        point = self.toMapCoordinates(self.canvas.mouseLastXY())
        point = list(point)
        print point

canvas_clicked = PrintClickedPoint( iface.mapCanvas() )
iface.mapCanvas().setMapTool( canvas_clicked )

Now I have called it in my code by :

canvas_clicked = PrintClickedPoint( self.iface.mapCanvas() )
iface.mapCanvas().setMapTool( canvas_clicked )

It is working fine and I am getting the point, but now I need to deactivate this tool and proceed with other code block written after that. How do I do that?

2
  • Try this iface.mapCanvas().unsetMapTool(canvas_clicked), see: api.qgis.org/api/…
    – Taras
    Commented Nov 14, 2022 at 12:01
  • Yes I have....but requirement is little different i want to unset the tool just after canvasPressEvent( self, e ): not before that..
    – Rpandia31
    Commented Nov 14, 2022 at 12:24

1 Answer 1

3

I guess you need overriding canvasReleaseEvent. Add it to the class definition.

def canvasReleaseEvent( self, e ):
    iface.mapCanvas().unsetMapTool( self )

After releasing the mouse button, the tool is deactivated.

Your Answer

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

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