You can use the setStyleFont()setStyleFont()
method of the QgsLayoutItemLegend
class. The first argument is a component, so you need to pass a QgsLegendStyle::StyleQgsLegendStyle::Style
enumerator item, e.g. QgsLegendStyle.Title
, QgsLegendStyle.SymbolLabel
etc.
Example snippet:
item_font = QFont('Arial', 14)
legend_item.setStyleFont(QgsLegendStyle.SymbolLabel, item_font)
legend_item.refresh()
Note that according to the docs, this method is deprecated and from version 3.30 onwards, you should
use QgsLegendStyle::setTextFormat()
QgsLegendStyle::setTextFormat()
from style()style()
instead.
Example below. FYI, with this approach I found it necessary to add a call to legend_item.setStyleMargin()
because, for some reason, the space between the symbol and label is lost which may be a bug, but I don't have time to investigate further right now.
# >= 3.30
legend_style = QgsLegendStyle()
text_format = QgsTextFormat()
item_font = QFont('Arial')
text_format.setFont(item_font)
text_format.setSize(14)
legend_style.setTextFormat(text_format)
legend_item.setStyle(QgsLegendStyle.SymbolLabel, legend_style)
legend_item.setStyleMargin(QgsLegendStyle.Symbol, QgsLegendStyle.Right, 2.5)
legend_item.refresh()