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?


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()
  • 1
    No don't do this. It will be bad for preformance. – Nathan W Aug 1 '14 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 '18 at 13:57
  • This will decrease performance for large dataset. – Ahsan Mukhtar Jun 28 '18 at 10:15

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