Using code I found at Go through items in a composer legend (QGIS 2.6) I can access the the layers in my legend fine. However, I have layers that are styled by category and I can't figure out how to change the category labels. I just want to replace the underscores in the category labels with a space.
After looking through the API I think I may be able to use the rendererV2 class. The code below is giving strange results. the first set of print
commands return 'foo', but the second set return the original labels and these are also printed on the output pdf.
Is there something I'm missing with the renderer? or, is there a better way to achieve this task?
I'm using QGIS 2.18.7 on windows 8
for legendLyr in iface.mapCanvas().layers():
if legendLyr.name() != "os1250_line" and legendLyr.name() != "os1250_text":
renderer = legendLyr.rendererV2()
if renderer.type() == "categorizedSymbol":
myRenderer = renderer.clone()
for cat in myRenderer.categories():
cat.setLabel("foo")
print(cat.label())
legendLyr.setRendererV2(myRenderer)
for cat in legendLyr.rendererV2().categories():
print (cat.label())
legendLyr.triggerRepaint()