4

Hi this may be more of a python question rather than qgis processing script question. I've managed to successfully get the output that I want from my processing script, but I wish to take one step further and style it automatically with a saved qml file.

The code below doesn't work because the return ends my script. How should I go about doing this?

return {self.OUTPUT: dest_id}
iface.activeLayer().loadNamedStyle('Buffer.qml')
iface.activeLayer().triggerRepaint()
TomazicM
27.3k25 gold badges33 silver badges43 bronze badges
asked Mar 1, 2019 at 11:16
1
  • Are you trying to set the style after the processing tool (from Python) or are you trying to set the style from within the processing tool itself? Commented Aug 31, 2019 at 9:48

1 Answer 1

1

You would need to create a subclass of QgsProcessingLayerPostProcessorInterface and apply the style in it's postProcessLayer method

First define the post processor class and create an an instance as follows

class LayerStyler(QgsProcessingLayerPostProcessorInterface):
 def postProcessLayer (self, layer, context, feedback):
 if layer.isValid():
 layer.loadNamedStyle('my_style.qml')
my_styler = LayerStyler()

Then set the post processor in processAlgorithm

def processAlgorithm():
 ...
 
 if context.willLoadLayerOnCompletion(dest_id):
 context.layerToLoadOnCompletionDetails(dest_id).setPostProcessor(my_styler)
 
 return {self.OUTPUT: dest_id}
answered Jun 14, 2021 at 12:36

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.