Skip to main content
Tweeted twitter.com/StackGIS/status/1546781418470129665
edited tags
Link
underdark
  • 84.5k
  • 22
  • 233
  • 415
spelling error
Source Link

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 stagestrange 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()

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 stage 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()

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()
added 40 characters in body; edited tags; edited title
Source Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345

How do I change Changing legend item text with pyQGIS?

Using code I found at hereGo 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 stage 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()

How do I change legend item text with pyQGIS?

Using code I found here 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 stage 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()

Changing legend item text with pyQGIS?

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 stage 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()
Source Link
Loading