My aim is to change the layer's name only within the legend - I want to leave the layer itself untouched. I tried multiple variants of X.setName(), both on the vector layer item, the map layer and the LayerTreeLayer and so on, via the model - All of that also changes the layer's name outside the legend, which is not what I want.
I found that within the Print Layout editor, there is actually a way to achieve this - you can click on the legend item, select the one you want to rename, "edit selected item properties", and then just enter the new label text. But how can I do this from code?
This is my code so far, using a rename of the base layer which is what I want to avoid.
legend = QgsLayoutItemLegend(layout) legend.setTitle("Legend") # vector_layer.setName("Layer Name in Legend") # change item name in legend legend.setAutoUpdateModel(False) # separates the layout's legend from the project's legend.setLinkedMap(map) legend.setLegendFilterByMapEnabled(True) layout.addLayoutItem(legend) legend.attemptMove(QgsLayoutPoint(235, 36, QgsUnitTypes.LayoutMillimeters))
I am working in QGIS 3.22.