I have a polygon layer with categorized symbology. I want to change the stroke width of all the categories without touching the categories or colours.
Here is the script I have after looking a similar questions
lyr = iface.activeLayer()
rndr = lyr.renderer()
sym = rndr.symbol()
syms.setStrokeWidth(3)
lyr.triggerRepaint()
I get the following error:
AttributeError: 'QgsCategorizedSymbolRenderer' object has no attribute
'symbol'
Best Answer
You can use the snippet below. I used an outline width of 0.5 in the example (3 is very wide).
Result of the example above on a test layer:
Or... you can also try the following, using the method
updateSymbols()
which according to the docs should:Both code snippets presented here worked fine for me in 3.20.
Edit 2: I played around with this a bit more for my own understanding. The two examples above work fine with a categorized renderer with one simple fill symbol layer for each category, but not if there are multiple symbol layers.
I came up with the script below which should change symbol outline but preserve any other fill symbol layers which are present.
In the screencast below, you can see I have multiple symbol layers for some of the categories, which are preserved after running the script while the outline stroke width is changed.