1

I would load to QGIS map a vector layer from processing script. I'm trying to use QgsProject.instance().addMapLayer(layer). It works if I run the code from pyqgis console:

layer=QgsVectorLayer('fileName.shp',"name","ogr")
if not layer.isValid():
   print('layer failed to load')
QgsProject.instance().addMapLayer(layer)

but it cannot works if I run the same from processing script. No error alerts are shown.

1 Answer 1

2

From a previous answer in another processing context, the following should do what you want

context.temporaryLayerStore().addMapLayer(layer)
context.addLayerToLoadOnCompletion(
            layer.id(),
            QgsProcessingContext.LayerDetails('SQL layer',
                                              context.project(),
                                              'LAYER'))

See both method temporaryLayerStore doc and method addLayerToLoadOnCompletion doc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.