I'm working with QGIS 3.40 (Bratislava) and I’m trying to automatically hide all layer names in a print-composer legend. Right now, I’m doing it manually: right-clicking each layer in the legend and selecting Hide, one by one. After that, I reorganize the layers inside a group to get the adaptive legend I want.
Since I’m generating an atlas and need to repeat this process very often, I’d like to automate it with PyQGIS.
I found a related topic here and I used a slightly different code: Setting layer name in composer legend as "Hidden" using PyQGIS?
from qgis.core import ( QgsProject, QgsLayoutItemLegend, QgsLegendRenderer, QgsLegendStyle, QgsLayerTreeNode ) # ------------------------------------------------------------------ # Fonction récursive : masque un nœud et tous ses nœuds descendants # ------------------------------------------------------------------ def hide_node_recursively(node): # Masquer le nœud lui-même QgsLegendRenderer.setNodeLegendStyle(node, QgsLegendStyle.Hidden) # Parcourir les enfants s'il y en a if hasattr(node, "children"): for child in node.children(): hide_node_recursively(child) # ------------------------------------------------------------------ # 1. Récupération du layout # ------------------------------------------------------------------ project = QgsProject.instance() manager = project.layoutManager() layout = manager.layoutByName('atlas') # Adapter si nécessaire if layout is None: raise Exception("Layout 'atlas' not found.") # ------------------------------------------------------------------ # 2. Récupération de la légende # ------------------------------------------------------------------ legends = [i for i in layout.items() if isinstance(i, QgsLayoutItemLegend)] if not legends: raise Exception("No legend found in layout.") legend = legends[0] # ------------------------------------------------------------------ # 3. Désactivation des mises à jour automatiques # ------------------------------------------------------------------ legend.setAutoUpdateModel(False) legend.updateLegend() # ------------------------------------------------------------------ # 4. Masquage récursif de TOUTE la légende # ------------------------------------------------------------------ model = legend.model() root = model.rootGroup() hide_node_recursively(root) # <-- récursion totale # ------------------------------------------------------------------ # 5. Rafraîchir la légende et le layout # ------------------------------------------------------------------ legend.refresh() layout.refresh() But it's interfering with the code I use to remove the "bande 1" in the print-composer with a code that was given to me on the forum (thanks again it's working well alone!) here : Automate the turn off QGIS band number in the legend
layout_name = "atlas" #Adjust to match the name of your layout #Find the layout object by its name lm = QgsProject.instance().layoutManager().layoutByName(layout_name) #List legends, pick the first (0) one. legend = [x for x in lm.items() if isinstance(x, QgsLayoutItemLegend)][0] #For each child in the legend, if it is a Legend Node, # change its label to a whitespace model = legend.model() children = list(model.children()) for child in children: if isinstance(child, QgsSimpleLegendNode): #print(child) #<QgsSimpleLegendNode: "Band 1: Layer_1 (Palette)"> #<QgsSimpleLegendNode: "Band 1 (Gray)"> child.setUserLabel(" ") Do you have an idea to them make both work together ?
Thanks a lot for your help!