I am trying to generate maps in PDF files by using a pre-defined .qgz
layout. I am already able to load layers and adjust color scale, etc. Since the legend is synchronized with the map layers, all the layers I have loaded are listed in the legend.
Using PrintComposer in QGIS I can turn off synchronization and remove the unwanted items in legend. Is there a way to do it automatically in Python? I tried to use QgsLegendModel
, but it also removes the layers from map canvas.
projectInstance= QgsProject.instance()
projectLayoutManager = projectInstance.layoutManager()
for comp in projectLayoutManager.printLayouts():
for item in comp.items():
if(isinstance(item, QgsLayoutItemLegend)):
model = item.model()
while model.rowCount() > 1:
model.removeRow(0)
break