I am working on a standalone PyQGIS script and I wanted to set the project extent to position on the 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 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 objects?
Is it meant to be like this because someone would want to work with native QGIS and someone with the standalone app or something else?