When I need to zoom to a feature, I have to select it first using layer.select(feature.id()) then I can zoom using mapCanvas.zoomToSelected(layer).

layer = iface.activeLayer()

QgsMapCanvas has only zoomToSelected() method related to a layer. My problem is that I should not change the currently selected features when I want to zoom to another feature.

Is there a way to zoom to a feature without selecting it?

1 Answer 1


You can use setExtent to the bounding box of the feature:

layer = iface.activeLayer()

feats = {f.id():f.geometry().boundingBox() for f in layer.getFeatures()}
canvas = iface.mapCanvas()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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