I am working on standalone pyqgis script and I wanted to set project extent to position on imported layer. First I tried with:
vlayer = QgsVectorLayer("data/layers/layer1.gpkg", "layer_name", "ogr") canvas= iface.mapCanvas() canvas.setExtent(layer.extent()) canvas.refresh()
And I got:
AttributeError: 'NoneType' object has no attribute 'mapCanvas'
Then I tried with using the same layer:
ex = vlayer.extent() canvas = QgsMapCanvas() canvas.setExtent(ex)
And then it worked.
I am wondering why i could do this using QgsMapCanvas and not using mapCanvas from iface(which i imported
from qgis.utils import iface).
What is the difference between these two object? Is it meant to be like this because someone would want to work with native Qgis and someone with standalone app or something else?