QgsLegendRenderer class

The QgsLegendRenderer class handles automatic layout and rendering of legend.

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

All spacing and sizes are in millimeters.

Public static functions

static auto nodeLegendStyle(QgsLayerTreeNode* node, QgsLayerTreeModel* model) -> QgsLegendStyle::Style
Returns the style for the given node, within the specified model.
static void setNodeLegendStyle(QgsLayerTreeNode* node, QgsLegendStyle::Style style)
Sets the style of a node.

Constructors, destructors, conversion operators

QgsLegendRenderer(QgsLayerTreeModel* legendModel, const QgsLegendSettings& settings)
Constructor for QgsLegendRenderer.

Public functions

void drawLegend(QPainter* painter)
Draws the legend with given painter.
void drawLegend(QgsRenderContext& context)
Draws the legend using a given render context.
auto legendSize() const -> QSizeF
Returns the preferred legend size set by the client.
auto minimumSize() -> QSizeF
Runs the layout algorithm and returns the minimum size required for the legend.
void setLegendSize(QSizeF s)
Sets the preferred resulting legend size.

Function documentation

static QgsLegendStyle::Style QgsLegendRenderer::nodeLegendStyle(QgsLayerTreeNode* node, QgsLayerTreeModel* model)

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

static void QgsLegendRenderer::setNodeLegendStyle(QgsLayerTreeNode* node, QgsLegendStyle::Style style)

Sets the style of a node.

QgsLegendRenderer::QgsLegendRenderer(QgsLayerTreeModel* legendModel, const QgsLegendSettings& settings)

Constructor for QgsLegendRenderer.

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

void QgsLegendRenderer::drawLegend(QPainter* painter)

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.

void QgsLegendRenderer::drawLegend(QgsRenderContext& context)

Draws the legend using a given render context.

The legend will occupy the area reported in legendSize().

QSizeF QgsLegendRenderer::legendSize() const

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.

QSizeF QgsLegendRenderer::minimumSize()

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

void QgsLegendRenderer::setLegendSize(QSizeF s)

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.