I have a QGIS python processing scripts that return multiple rasters. I want to return them in a group named 'Aligned'. There is nothing in the context.addLayerToLoadOnCompletion() function to specify group.
I tried doing something like this to achieve what I want but it didn't work. It did create a group at the end of all layers in the layers panel, but the outputs were still added outside the group.
root = context.project().instance().layerTreeRoot()
group = root.addGroup('Aligned')
context.addLayerToLoadOnCompletion(
outputs["Runoff Local"]["OUTPUT"],
QgsProcessingContext.LayerDetails(
f"Runoff Local (L) ", context.project(), "Runoff Local"
),
)
Is there a way to cleanly group all layers from the script in one group?
Best Answer
I was able to achieve this and this is my understanding:
If you do it through
postProcessAlgorithm
, QGIS will natively output layer as well, so you will have duplicate layers in your Canvas. SopostProcessAlgorithm
is not the best approach.This is how I did it using the native QGIS output, rather than adding a layer to the canvas through my algorithm.
I first created a group through postProcessAlgorithm using Ben W's answer. This only creates and selects a group. Does not add anything.
Selection function made using the help of this Selecting subgroup in Layers panel using PyQGIS