I'm developping a Python standalone app that needs a dynamic map (with the zoom and pan tools).
I chose to use PyQGIS that allows me to work with QGIS projects. I managed to create a class that takes a path to a project, loads it and displays it. However, by default, the largest layer is shown which may be a problem.
My QGIS project (that can vary, it depends on the needs) has the world's map as its largest layer with some smaller layers, for example a specific town. I want to give to my class a layer (throught a name or id) that will be shown by default (the canvas will be zoomed to that layer).
How can I do it ?
I want to add that, obviously, I searched for a solution before asking my question and tried some options with the QgsMapCanvas' methods (zoomToFeatureExtent and the more simple method zoomIn). But I can't make it work. I don't get any error whatsoever. If I check the extent of the canvas, it's well set (the same as the layer i chose) but I don't have the wanted result on the screen. I don't know what I'm doing wrong.
Here's the code I have that show my QGIS project in a canvas (I deleted the part where i add the zoom and pan tool to make it less long, I don't think it's needed) :
class MyWindow(QMainWindow): def __init__(self,path): QMainWindow.__init__(self) self.canvas = QgsMapCanvas() self.canvas.setCanvasColor(Qt.white) self.project = QgsProject.instance() self.bridge = QgsLayerTreeMapCanvasBridge(self.project.layerTreeRoot(), self.canvas) self.project.read(path) self.canvas.freeze(True) self.canvas.refresh() self.canvas.freeze(False) self.canvas.repaint() self.setCentralWidget(self.canvas)