I created a QPT model with a map inside QGIS. The problem is when I try to select the map under pyQGIS :
qpt = QDomDocument() qpt.setContent(content) layout = QgsLayout(QgsProject().instance()) layout.loadFromTemplate(qpt, QgsReadWriteContext()) mapItem = layout.itemById('Map') # 'Map' is the name of the item map mapCanvas = iface.mapCanvas() mapItem.zoomToExtent(mapCanvas.extent())
It returns this error :
AttributeError: 'QgsLayoutItem' object has no attribute 'zoomToExtent'
I know there is a class
QgsLayoutItemMap but I don't know how to get it from the QgsLayoutItem map.
And I have another question : in QGIS composer, there is a button when I select my Map called "Set to map canvas extent". What is the equivalent in python ?
mapItem.zoomToExtent(mapCanvas.extent()) and then