from qgis.gui import QgsMapCanvas canvas = QgsMapCanvas()
from qgis.core import QgsProject, QgsRasterLayer from qgis.gui import QgsMapCanvas, QgsMapLayerComboBox canvas = QgsMapCanvas() # Add layers to the project layer_path = '/path/to/raster/layer.tif' layer_name = 'my_raster_layer' project = QgsProject.instance() layer = QgsRasterLayer(layer_path, layer_name) project.addMapLayer(layer) # Set the layer on the canvas and set the extent canvas.setExtent(layer.extent()) canvas.setLayers([layer])
from qgis.gui import QgsMapToolPan, QgsMapToolZoom canvas = QgsMapCanvas() # Add layers to the canvas # Set up map tools pan_tool = QgsMapToolPan(canvas) zoom_tool = QgsMapToolZoom(canvas) # Add the map tools to the canvas canvas.setMapTool(pan_tool) canvas.setMapTool(zoom_tool)This example creates a map canvas widget and adds two map tools: a pan tool and a zoom tool, allowing the user to interact with the map on the canvas. In summary, QgsMapCanvas is a class in the qgis.gui package library of Python, used to create a map canvas widget in a GUI application, and provides interactivity and displays maps.