[QGIS-Developer] Load layer with style in Processing plugin

Nyall Dawson nyall.dawson at gmail.com
Wed May 30 20:04:27 PDT 2018

On 30 May 2018 at 20:22, matteo <matteo.ghetta at gmail.com> wrote:
> Hi devs,
> is it possible to load a layer (vector or raster) created in a
> Processing script/algorithm from the script itself without using the
> specific algorithm "set style for raster layer"?
> I'm using context.addLayerToLoadOnCompletion() to load the layers and I
> have a separated function that creates the style (as qml file with the
> same name of the layer). But when the layers are loaded the styles
> aren't (even if correctly created in the folder and it works if manually
> loaded in QGIS).
> Thanks for any suggestion

In 3.2 it should be possible, using a subclass of
QgsProcessingLayerPostProcessorInterface and

    context.layerToLoadOnCompletionDetails( output_dest_id
).setPostProcessor( my_layer_post_processor() )

Your QgsProcessingLayerPostProcessorInterface subclass would set the
style for the layer in its postProcessLayer implementation.

This is the canonical, thread safe way to do this in processing... any
other approach (and there's other hacky ways to do this) will not be
thread safe and may crash! We probably should make this a bit easier
and have a direct `context.layerToLoadOnCompletionDetails(
output_dest_id ).setLayerStyle(..... )` call, but it's too late for


More information about the QGIS-Developer mailing list