I recently updated my QGIS and I noticed the styles now show the band Band 1(Gray)
The issue is it now shows within my print layout:
Using PYQGIS, how can I remove just the Band 1 (Gray)
?
For reference, here is how I am currently setting the legend in the layout:
def set_legend(layout: QgsPrintLayout, tree: QgsLayerTree, layer: QgsLayer, item_id: str):
'''Sets the Legend items'''
logging.info(f'setting legend: {item_id}')
item = layout.itemById(item_id)
# set layer as root for legend
tree.addLayer(layer)
item.model().setRootGroup(tree)
node = item.model().rootGroup().findLayer(layer)
# hide the node title
QgsLegendRenderer.setNodeLegendStyle(node, QgsLegendStyle.Hidden)
Thank you!
Here's my solution after digging StackOverflow and the API:
root = model.rootGroup().findLayer(layer)
# hide the node with label: Band 1 (Gray)
if isinstance(layer, QgsRasterLayer):
nodes = model.layerLegendNodes(root)
if nodes[0].data(0) == 'Band 1 (Gray)':
indexes = list(range(1, len(nodes)))
QgsMapLayerLegendUtils.setLegendNodeOrder(root, indexes)
model.refreshLayerLegend(root)