I am in the process of creating a script which needs to add a caption to an already existing layout.
So I created a PyQGIS code in order to do it :
legend = QgsLayoutItemLegend(layout) layout.addLayoutItem(legend) legend.setLinkedMap(map) # pass a QgsLayoutItemMap object legend.setLegendFilterByMapEnabled(True) #legend.adjustBoxSize() legend.setResizeToContents(False) legend.attemptMove(QgsLayoutPoint(3.5, 12, QgsUnitTypes.LayoutMillimeters)) legend.attemptResize(QgsLayoutSize(5.5, 2, QgsUnitTypes.LayoutCentimeters)) legend.setLineSpacing(15) legend.setSymbolHeight(6) #legend.setSpacingAboveGroups(15)
The legend is displayed well but I would like there to be space before the different groups of the legend.
I saw that this was possible in the layout editor (screenshot) but after studying the documentation for
QgsLayoutItemLegend, I can't find a "
setSpacingAboveGroups()" method that would allow this value to be modified. (
setSpacingAboveGroups does not exist, this is an example)
How can I change the distance between groups in the legend using PyQGIS?