I am using a script to change the opacity on a rule-based symbology.

layer = iface.activeLayer()
opacity = 0.2

if layer.renderer().type() ==  'RuleRenderer': 
    renderer = layer.renderer()     #QgsRuleBasedRenderer 
    rule = renderer.rootRule() # QgsRuleBasedRenderer.Rule
    chil = rule.children()
    for i in range(len(chil)):        
        chil [chil.index(chil[i])].symbol().setOpacity(opacity)
        # apply the renderer to the layer


The script works fine and I can see the new symbol with the correct opacity both in the canvas and in the TOC but not in the layer styling panel. Here the symbology remains unmodified and when I click on one of the rules to edit it, the opacity is still at the old value. Also, when closing the edit rule panel, the old opacity comes back on TOC and Canvas.

Any suggestion on how to make the changes permanent?

  • Yes, it was my error during the copy of the code. I will correct the question. Thanks
    – Val P
    Oct 12, 2021 at 19:51

1 Answer 1


Nice question!

At the end of your script you need to add:


which will notify the "Layer Styling" panel about any changes you've made to the layer style.

  • 1
    Of course! Excellent solution. I was looking for a similar trigger but I couldn't figure it out. Still a lot to learn. Much appreciated.
    – Val P
    Oct 8, 2021 at 4:35

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.