12

What are the commands to apply all types of zoom (full, on selected layer, etc.) in python console? I'm searching both on pyqgis cookbook and API documentantion, but I'm not finding anything interesting.

2

2 Answers 2

19

After getting around cookbook, the best place to learn is referring the QGis API Documentation. In this case we are looking for zoom functions, which are related to map canvas. So check if QgsMapCanvas class contains something.

enter image description here

Zoom functions available in QGis are provided in QgsMapCanvas Class.

>>> canvas = qgis.utils.iface.mapCanvas()
>>> canvas.zoomIn()
>>> canvas.zoomOut()
>>> canvas.zoomToFullExtent()
2
  • How to you get to qgis.utils.iface.mapCanvas() from QgsMapCanvas? I tried to to a = QgsMapCanvas() and then a.zoomIn() but it doesnt do anything. Thanks May 7, 2021 at 9:03
  • You can just use iface.mapCanvas(). Jul 15, 2021 at 14:24
6

All the zoom options under the 'View' menu can be executed in the python console by the following code.

eMenu = self.iface.viewMenu()
eMenu.actions() [index].trigger()

Replace the "index" with the index number of the zoom action you want to perform. For eg., To perform 'zoom to selected features', use

eMenu.actions() [12].trigger()
2
  • 2
    How do you know that the number 12 denotes 'zoom to selected features'? Jan 11, 2017 at 3:28
  • You better use iface.actionZoomToSelected().trigger() instead! Jul 15, 2021 at 14:27

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.