Class: QgsLegendRenderer

Handles automatic layout and rendering of legends.

The content is given by QgsLayerTreeModel instance. Various layout properties can be configured within QgsLegendRenderer.

All spacing and sizes are in millimeters.

Methods

drawLegend

Draws the legend with given painter.

exportLegendToJson

Renders the legend in a json object.

legendSize

Returns the preferred legend size set by the client.

minimumSize

Runs the layout algorithm and returns the minimum size required for the legend.

proxyModel

Returns the filter proxy model used for filtering the legend model content during rendering.

setLegendSize

Sets the preferred resulting legend size.

Static Methods

nodeLegendStyle

Returns the style for the given node, within the specified model.

setNodeLegendStyle

Sets the style of a node.

class qgis.core.QgsLegendRenderer[source]

Bases: object

__init__(legendModel: QgsLayerTreeModel | None, settings: QgsLegendSettings)

Constructor for QgsLegendRenderer. The ownership of the legend model is not changed, and the model must exist for the lifetime of this renderer.

Parameters:
drawLegend(self, painter: QPainter | None)[source]

Draws the legend with given painter. The legend will occupy the area reported in legendSize(). The painter should be scaled beforehand so that units correspond to millimeters.

Deprecated since version 3.40: Use the variant which accepts a QgsRenderContext instead.

Parameters:

painter (Optional[QPainter])

drawLegend(self, context: QgsRenderContext)[source]

Draws the legend using a given render context. The legend will occupy the area reported in legendSize().

Added in version 3.6.

Parameters:

context (QgsRenderContext)

exportLegendToJson(self, context: QgsRenderContext) Dict[str, QJsonValue][source]

Renders the legend in a json object.

Added in version 3.8.

Parameters:

context (QgsRenderContext)

Return type:

Dict[str, QJsonValue]

legendSize(self) QSizeF[source]

Returns the preferred legend size set by the client.

If the returned size is null, the legend will be drawn with the minimum possible size to fit its content.

See also

minimumSize()

See also

setLegendSize()

Return type:

QSizeF

minimumSize(self, renderContext: QgsRenderContext | None = None) QSizeF[source]

Runs the layout algorithm and returns the minimum size required for the legend.

See also

setLegendSize()

See also

legendSize()

Parameters:

renderContext (Optional[QgsRenderContext] = None)

Return type:

QSizeF

static nodeLegendStyle(node: QgsLayerTreeNode | None, model: QgsLayerTreeModel | None) Qgis.LegendComponent[source]

Returns the style for the given node, within the specified model.

Parameters:
Return type:

Qgis.LegendComponent

proxyModel(self) QgsLayerTreeFilterProxyModel | None[source]

Returns the filter proxy model used for filtering the legend model content during rendering.

Filters can be set on the proxy model to filter rendered legend content.

Added in version 3.40.

Return type:

Optional[QgsLayerTreeFilterProxyModel]

setLegendSize(self, s: QSizeF)[source]

Sets the preferred resulting legend size.

If the size is null, the legend will be drawn with the minimum possible size to fit its content.

See also

legendSize()

See also

minimumSize()

Parameters:

s (QSizeF)

static setNodeLegendStyle(node: QgsLayerTreeNode | None, style: Qgis.LegendComponent)[source]

Sets the style of a node.

Parameters: