Skip to content

QgisCanvas

Import
from pyqgis_wrapper.application import QgisCanvas

QgisCanvas(qgis, canvas_name='PyQGIS Map Viewer')

Bases: qgis.PyQt.QtWidgets.QMainWindow

Custom canvas to be executed within your IDE

TODO: Add a button or a right click on a layer menu to zoom to the layer extent.

Parameters:

Name Type Description Default
qgis qgis.core.QgsApplication

Application in which the canvas will be launched

required
canvas_name str

Title for the canvas, defaults to "PyQGIS Map Viewer"

'PyQGIS Map Viewer'
Source code in pyqgis_wrapper/application/qgis_app.py
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
def __init__(self, qgis: QgsApplication, canvas_name: str = "PyQGIS Map Viewer"):
    """
    :param qgis: Application in which the canvas will be launched
    :type qgis: QgsApplication
    :param canvas_name: Title for the canvas, defaults to "PyQGIS Map Viewer"
    :type canvas_name: str, optional
    """
    super().__init__()
    self.qgis = qgis
    self.setWindowTitle(canvas_name)
    self.setGeometry(250, 150, 1000, 750)

    self.canvas = QgsMapCanvas(self)
    self._create_bridge()
    self._layout()
    self._triggers()
    self._nav_actions()

show property

Launch the canvas.

Current instance can't be modified after.

on_layer_selected(layer)

When user selects a layer in the tree

Source code in pyqgis_wrapper/application/qgis_app.py
369
370
371
372
373
374
def on_layer_selected(self, layer):
    """
    When user selects a layer in the tree
    """
    if layer and layer.isValid():
        self.canvas.setCurrentLayer(layer)

load_layers(layers)

Load layers in a QgsTreeLayer (legend)

Parameters:

Name Type Description Default
layers typing.Union[qgis.core.QgsMapLayer, typing.List[qgis.core.QgsMapLayer]]

description

required
Source code in pyqgis_wrapper/application/qgis_app.py
408
409
410
411
412
413
414
415
416
417
418
def load_layers(self, layers: Union[QgsMapLayer, List[QgsMapLayer]]):
    """
    Load layers in a QgsTreeLayer (legend)

    :param layers: _description_
    :type layers: Union[QgsMapLayer, List[QgsMapLayer]]
    """
    self._add_layers(layers)

    for layer in self.layers:
        self.project.instance().addMapLayer(layer)

load_project(project)

Load the layers from a QgsProject in a QgsTreeLayer (legend)

Parameters:

Name Type Description Default
project str

Project to load

required
Source code in pyqgis_wrapper/application/qgis_app.py
420
421
422
423
424
425
426
427
428
429
430
def load_project(self, project: str):
    """
    Load the layers from a QgsProject in a QgsTreeLayer (legend)

    :param project: Project to load
    :type project: QgsProject
    """
    self.project.read(project)
    layers = list(self.project.mapLayers().values())

    self._add_layers(layers)

closeEvent(event)

Intercept the close event and preserve layers by taking ownership.

Source code in pyqgis_wrapper/application/qgis_app.py
445
446
447
448
449
450
451
452
453
454
455
456
457
458
def closeEvent(self, event):
    """
    Intercept the close event and preserve layers by taking ownership.
    """
    # Detach layers from project to give ownership to QgisCanvas
    layers = []
    for layer in self.layers:
        layer = self.project.takeMapLayer(layer)
        layers.append(layer)

    # Close the project singleton
    self.project.clear()

    super().closeEvent(event)