[GIS] Setting the PyQGIS Maptool

pyqgis

I apologize if this question was already answered, but I found a lot of examples on how to set the QGIS maptool in a application from scratch, but not inside QGIS (in a plugin).

In the examples I found, the author creates a toolbar icon, an action and a custom QgsMapTool action.

What I want to do, is something (that I thought it was) more simple:
– I want to set the canvas maptool to be the QGIS native zoom in (QgsMapToolZoom); basically the same that happens, when the user clicks the zoom in icon on the QGIS toolbar;
Note that I don't want to zoom in like this:

canvas.zoomIn()

But I want to set the current mapTool to zoom in, and let the user zoom instead.

I implemented it as follows:

        canvas=self.eaf.iface.mapCanvas()

        zoomTool=QgsMapToolZoom(canvas, False)

        canvas.setMapTool(zoomTool)

However this fails. QGIS unsets the current maptool (lets say, for instance pan) and sets a void maptool (represented by an arrow cursor and no action)
I added this piece of code, after initializing the zoom tool:

        zoomTool=QgsMapToolZoom(canvas, False)

        act=zoomTool.action()
        print act            

And found out that the action of the tool is empty (None).
I don't want to start an action, because I would like to use the native QGIS actions that are associated to the QGIS toolbar icons.
How can I access the native QGIS zoom action and force my maptool to use it?

Thanks in advance for your time

Best Answer

You get access to the native mpatool actions via several iface.actionXXX() methods which you can trigger. For your case this is

iface.actionZoomIn().trigger()

or

iface.actionZoomOut().trigger()
Related Question