Regarding my plugin pyqgis 2.2, i can load a vector layer succesfully. But when i try to zoom to the layer extent it does not work.

How to do in order to zoom to the layer extent?

I have just tried your proposition.I pasted your piece of code into my plugin. But unfortunatly, when i load a vector layer. I try to zoom to the layer extent (thanks to right click -> zoom to layer extent). I have no error message but there 's no change zoom to the extent of vector layer.

You can read the python.py (github plugin link) here :


Any thoughts?


2 Answers 2


Something like below should work.

vLayer = iface.activeLayer()
canvas = iface.mapCanvas()
extent = vLayer.extent()

One way to do it would be to select all features in the layer, zoom to selected and then deselect everything:

use this code:

mCanvas = iface.mapCanvas()
  • 2
    No don't do this. It will be bad for preformance.
    – Nathan W
    Aug 1, 2014 at 9:08
  • I think this result is better as when I use OTF transformation, the top rated answer cannot zoom to the extent in transformed CRS.
    – natsuapo
    Apr 12, 2018 at 13:57
  • 1
    This will decrease performance for large dataset. Jun 28, 2018 at 10:15
  • If you need to zoom to an extent in a different CRS, best just transform the extent instead of using this working, but highly inefficient approach. Dec 7, 2022 at 11:07

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