Saturday 6 August 2016

Setting layer name in composer legend as "Hidden" using PyQGIS?


Is there a way to hide a layer name in the composer legend using pyqgis as we do from the legend properties (see picture)?


enter image description here


The only way I've found so far is by changing the layer name in the composer legend but it will also change it in the Layers list and I don't want that.


This is the code I used:


        itemLlegend = composition.getComposerItemById('Legend')
modelindex=itemLlegend.modelV2().index(0, 0)
itemLlegend.modelV2().setFlag(QgsLayerTreeModel.AllowNodeRename)
itemLlegend.modelV2().setData(modelindex,'')
itemLlegend.updateLegend()


Any idea?



Answer



I had this problem too. My friend who is working for QGIS told me how.


What you can do is:


take the QgsLayerTreeLayer from the model (if you haven't include it in the model):


tree_layer = itemLlegend.modelV2().rootGroup().addLayer(layer)

If your layer is already in the model, just iterate rootGroup and grab your tree_layer.


After you got your tree layer:



from qgis.core import QgsLegendRenderer, QgsComposerLegendStyle
QgsLegendRenderer.setNodeLegendStyle(
tree_layer, QgsComposerLegendStyle.Hidden)

This will add style to your tree_layer it will now will be hidden by the renderer (but, just the Visitors text will be hidden).


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...