Is there a PyQGIS code to edit the transparency of rule based symbols?
-
3You can change the opacity levels of your symbols with .setOpacity() docs.qgis.org/3.22/en/docs/pyqgis_developer_cookbook/…wanderzen– wanderzen2022年10月12日 08:04:53 +00:00Commented Oct 12, 2022 at 8:04
1 Answer 1
As described in the @wanderzen's comment to this question you can use setOpacity()
to change the transparency of a symbol.
Symbols themselves can be accessed from each of the rules (QgsRuleBasedRenderer::Rule
) stored by the QgsRuleBasedRenderer
as children of the invisible root rule.
An example to set all all rules to 75% opacity would be:
layer = iface.activeLayer()
root_rule = layer.renderer().rootRule()
for rule in root_rule.children():
rule.symbol().setOpacity(0.75)
layer.triggerRepaint()
-
I don't manage to make your code work. I get this error: 'QgsSingleSymbolRenderer' object has no attribute 'rootRule'zakros– zakros2023年03月01日 16:27:56 +00:00Commented Mar 1, 2023 at 16:27
-
@zakros This question is about changing opacity of the individual symbols of a rule based renderer. If you are using a single symbol renderer you can just use
layer.setOpacity(0.75)
CodeBard– CodeBard2023年03月01日 19:08:02 +00:00Commented Mar 1, 2023 at 19:08 -
Thanks for answering me. Yes but if I use setOpacity I can't change the opacity of only the selectedFeatureszakros– zakros2023年03月01日 20:34:36 +00:00Commented Mar 1, 2023 at 20:34
-
I tried this:
vlayer = iface.activeLayer()
vlayer.selectByExpression('"field_A"= \'category_FZ15\'', QgsVectorLayer.SetToSelection)
vlayer.selectedFeatures()[0].setOpacity(1)
` But I get this:AttributeError: 'QgsFeature' object has no attribute 'setOpacity'
zakros– zakros2023年03月02日 09:36:03 +00:00Commented Mar 2, 2023 at 9:36
Explore related questions
See similar questions with these tags.