I am trying to get a layout to autogenerate as there is a certain set of information that I have to produce layouts for hundreds of times. I got the map, legend, and rest of the information to generate but the Legend is too large. It needs to only show the items in the map. I tried to do that manually but it still shows all the styles for each layer. Some of the layers used have 10+ styles so this is causing issues.
I was trying to use QgsLegendFilterButton as that is what I used when making a manual layout map but I can't seem to get that working in pyqgis. I have have been using guides to get the rest setup but couldn't find any on this so I'm totally lost at how to use QgsLegendFilterButton. Can anyone post a simple example of adding it to the code below? (if possible)
legend = QgsLayoutItemLegend(layout) legend.setTitle("Legend") layerTree = QgsLayerTree() layerTree.addLayer(layer) layers = QgsProject.instance().mapLayersByName('Cable') layer = layers layerTree.addLayer(layer) layers = QgsProject.instance().mapLayersByName('Attachment') layer = layers layerTree.addLayer(layer) legend.model().setRootGroup(layerTree) newFont = QFont("Comic Sans MS", 6) legend.setStyleFont(QgsLegendStyle.Title,newFont) legend.setStyleFont(QgsLegendStyle.Subgroup, newFont) legend.setStyleFont(QgsLegendStyle.SymbolLabel, newFont layout.addLayoutItem(legend) legend.attemptMove(QgsLayoutPoint(150,75, QgsUnitTypes.LayoutMillimeters))