root = QgsProject.instance().layerTreeRoot() for node in root.children(): print(node.name())
root = QgsProject.instance().layerTreeRoot() uri = "file:///path/to/shapefile.shp" layer_name = "My new layer" layer = QgsVectorLayer(uri, layer_name, "ogr") root.addLayer(layer)This code snippet creates a new vector layer using the QgsVectorLayer class with the path to the shapefile, the layer name, and the provider type ("ogr"). It then gets the layer tree root node with QgsProject.instance().layerTreeRoot() and adds the new layer to the root with root.addLayer(layer). Package library: qgis.core