Class: QgsLayoutItemLegend

A layout item subclass for map legends.

Class Hierarchy

Inheritance diagram of qgis.core.QgsLayoutItemLegend

Base classes

QgsLayoutItem

Base class for graphical items within a QgsLayout.

QgsLayoutObject

A base class for objects which belong to a layout.

QObject

QgsExpressionContextGenerator

Abstract interface for generating an expression context.

QGraphicsRectItem

QAbstractGraphicsShapeItem

QGraphicsItem

QgsLayoutUndoObjectInterface

Interface for layout objects which support undo/redo commands.

class qgis.core.QgsLayoutItemLegend[source]

Bases: QgsLayoutItem

__init__(layout: QgsLayout | None)

Constructor for QgsLayoutItemLegend, with the specified parent layout.

Parameters:

layout (Optional[QgsLayout])

adjustBoxSize(self)[source]

Sets the legend’s item bounds to fit the whole legend content.

autoUpdateModel(self) bool[source]

Returns whether the legend content should auto update to reflect changes in the project’s layer tree.

Return type:

bool

boxSpace(self) float[source]

Returns the legend box space.

See also

setBoxSpace()

Return type:

float

columnCount(self) int[source]

Returns the legend column count.

See also

setColumnCount()

Return type:

int

columnSpace(self) float[source]

Returns the legend column spacing.

See also

setColumnSpace()

Return type:

float

static create(layout: QgsLayout | None) QgsLayoutItemLegend | None[source]

Returns a new legend item for the specified layout.

The caller takes responsibility for deleting the returned object.

Parameters:

layout (Optional[QgsLayout])

Return type:

Optional[QgsLayoutItemLegend]

drawRasterStroke(self) bool[source]

Returns whether a stroke will be drawn around raster symbol items.

Return type:

bool

equalColumnWidth(self) bool[source]

Returns whether column widths should be equalized.

Return type:

bool

filterByMapItems(self) List[QgsLayoutItemMap]

Returns the maps to use when filtering legend content by map extents.

See also

setLinkedMap()

Added in version 3.32.

Return type:

List[QgsLayoutItemMap]

fontColor(self) QColor[source]

Returns the legend font color.

See also

setFontColor()

Deprecated since version 3.40: Use QgsLegendStyle.setTextFormat() from style() instead.

Return type:

QColor

legendFilterByMapEnabled(self) bool[source]

Find out whether legend items are filtered to show just the ones visible in the associated map

Return type:

bool

legendFilterOutAtlas(self) bool[source]

Returns whether to filter out legend elements outside of the current atlas feature.

Return type:

bool

legendSettings(self) QgsLegendSettings

Returns the legend’s renderer settings object.

Return type:

QgsLegendSettings

lineSpacing(self) float[source]

Returns the spacing in-between lines in layout units.

See also

setLineSpacing()

Deprecated since version 3.40: Use QgsLegendStyle.textFormat() from style() instead.

Return type:

float

linkedMap(self) QgsLayoutItemMap | None[source]

Returns the associated map.

See also

setLinkedMap()

Return type:

Optional[QgsLayoutItemMap]

maximumSymbolSize(self) float[source]

Returns the maximum symbol size (in mm). 0.0 means there is no maximum set.

Added in version 3.16.

Return type:

float

minimumSymbolSize(self) float[source]

Returns the minimum symbol size (in mm). A value 0.0 means there is no minimum set.

Added in version 3.16.

Return type:

float

model(self) QgsLegendModel | None[source]

Returns the legend model.

Return type:

Optional[QgsLegendModel]

rasterStrokeColor(self) QColor[source]

Returns the stroke color for the stroke drawn around raster symbol items. The stroke is only drawn if drawRasterStroke() is True.

Return type:

QColor

rasterStrokeWidth(self) float[source]

Returns the stroke width (in layout units) for the stroke drawn around raster symbol items. The stroke is only drawn if drawRasterStroke() is True.

Return type:

float

resizeToContents(self) bool[source]

Returns whether the legend should automatically resize to fit its contents.

Return type:

bool

rstyle(self, s: QgsLegendStyle.Style) QgsLegendStyle

Returns reference to modifiable legend style.

Parameters:

s (QgsLegendStyle.Style)

Return type:

QgsLegendStyle

setAutoUpdateModel(self, autoUpdate: bool)[source]

Sets whether the legend content should auto update to reflect changes in the project’s layer tree.

Parameters:

autoUpdate (bool)

setBoxSpace(self, space: float)[source]

Sets the legend box space.

See also

boxSpace()

Parameters:

space (float)

setColumnCount(self, count: int)[source]

Sets the legend column count.

See also

columnCount()

Parameters:

count (int)

setColumnSpace(self, spacing: float)[source]

Sets the legend column spacing.

See also

columnSpace()

Parameters:

spacing (float)

setDrawRasterStroke(self, enabled: bool)[source]

Sets whether a stroke will be drawn around raster symbol items.

Parameters:

enabled (bool) – set to True to draw borders

setEqualColumnWidth(self, equalize: bool)[source]

Sets whether column widths should be equalized.

Parameters:

equalize (bool)

setFilterByMapItems(self, maps: Iterable[QgsLayoutItemMap])[source]

Sets the maps to use when filtering legend content by map extents.

See also

setLinkedMap()

Added in version 3.32.

Parameters:

maps (Iterable[QgsLayoutItemMap])

setFontColor(self, color: QColor | Qt.GlobalColor)[source]

Sets the legend font color.

See also

fontColor()

Deprecated since version 3.40: Use QgsLegendStyle.setTextFormat() from style() instead.

Parameters:

color (Union[QColor, Qt.GlobalColor])

setLegendFilterByMapEnabled(self, enabled: bool)[source]

Set whether legend items should be filtered to show just the ones visible in the associated map.

Parameters:

enabled (bool)

setLegendFilterOutAtlas(self, doFilter: bool)[source]

When set to True, during an atlas rendering, it will filter out legend elements where features are outside the current atlas feature.

Parameters:

doFilter (bool)

setLineSpacing(self, spacing: float)[source]

Sets the spacing in-between multiple lines.

See also

lineSpacing()

Deprecated since version 3.40: Use QgsLegendStyle.setTextFormat() from style() instead.

Parameters:

spacing (float)

setLinkedMap(self, map: QgsLayoutItemMap | None)[source]

Sets the map to associate with the legend.

See also

linkedMap()

Parameters:

map (Optional[QgsLayoutItemMap])

setMaximumSymbolSize(self, size: float)[source]

Set the maximum symbol size for symbol (in millimeters).

A symbol size of 0.0 indicates no maximum is set.

Added in version 3.16.

Parameters:

size (float)

setMinimumSymbolSize(self, size: float)[source]

Set the minimum symbol size for symbol (in millimeters).

A symbol size of 0.0 indicates no minimum is set.

Added in version 3.16.

Parameters:

size (float)

setRasterStrokeColor(self, color: QColor | Qt.GlobalColor)[source]

Sets the stroke color for the stroke drawn around raster symbol items. The stroke is only drawn if drawRasterStroke() is True.

Parameters:

color (Union[QColor, Qt.GlobalColor])

setRasterStrokeWidth(self, width: float)[source]

Sets the stroke width for the stroke drawn around raster symbol items. The stroke is only drawn if drawRasterStroke() is True.

Parameters:

width (float)

setResizeToContents(self, enabled: bool)[source]

Sets whether the legend should automatically resize to fit its contents.

Parameters:

enabled (bool) – set to False to disable automatic resizing. The legend frame will not be expanded to fit legend items, and items may be cropped from display.

setSplitLayer(self, enabled: bool)[source]

Sets whether the legend items from a single layer can be split over multiple columns.

See also

splitLayer()

Parameters:

enabled (bool)

setStyle(self, component: QgsLegendStyle.Style, style: QgsLegendStyle)[source]

Sets the style of component to style for the legend.

Parameters:
setStyleFont(self, component: QgsLegendStyle.Style, font: QFont)[source]

Sets the style font for a legend component.

See also

styleFont()

Deprecated since version 3.40: Use QgsLegendStyle.setTextFormat() from style() instead.

Parameters:
setStyleMargin(self, component: QgsLegendStyle.Style, margin: float)[source]

Set the margin for a legend component.

setStyleMargin(self, component: QgsLegendStyle.Style, side: QgsLegendStyle.Side, margin: float) Set the margin for a particular side of a legend component.

Parameters:
setSymbolAlignment(self, alignment: Qt.AlignmentFlag)[source]

Sets the alignment for placement of legend symbols.

Only Qt.AlignLeft or Qt.AlignRight are supported values.

Added in version 3.10.

Parameters:

alignment (Qt.AlignmentFlag)

setSymbolHeight(self, height: float)[source]

Sets the legend symbol height.

See also

symbolHeight()

Parameters:

height (float)

setSymbolWidth(self, width: float)[source]

Sets the legend symbol width.

See also

symbolWidth()

Parameters:

width (float)

setTitle(self, title: str | None)[source]

Sets the legend title.

See also

title()

Parameters:

title (Optional[str])

setTitleAlignment(self, alignment: Qt.AlignmentFlag)[source]

Sets the alignment of the legend title.

See also

titleAlignment()

Parameters:

alignment (Qt.AlignmentFlag)

setWmsLegendHeight(self, height: float)[source]

Sets the WMS legend height.

Parameters:

height (float)

setWmsLegendWidth(self, width: float)[source]

Sets the WMS legend width.

See also

wmsLegendWidth()

Parameters:

width (float)

setWrapString(self, string: str | None)[source]

Sets the legend text wrapping string.

See also

wrapString()

Parameters:

string (Optional[str])

splitLayer(self) bool[source]

Returns whether the legend items from a single layer can be split over multiple columns.

See also

setSplitLayer()

Return type:

bool

style(self, s: QgsLegendStyle.Style) QgsLegendStyle[source]

Returns legend style.

Parameters:

s (QgsLegendStyle.Style)

Return type:

QgsLegendStyle

styleFont(self, component: QgsLegendStyle.Style) QFont[source]

Returns the font settings for a legend component.

See also

setStyleFont()

Deprecated since version 3.40: Use QgsLegendStyle.textFormat() from style() instead.

Parameters:

component (QgsLegendStyle.Style)

Return type:

QFont

symbolAlignment(self) Qt.AlignmentFlag[source]

Returns the alignment for placement of legend symbols.

Only Qt.AlignLeft or Qt.AlignRight are supported values.

Added in version 3.10.

Return type:

Qt.AlignmentFlag

symbolHeight(self) float[source]

Returns the legend symbol height.

Return type:

float

symbolWidth(self) float[source]

Returns the legend symbol width.

See also

setSymbolWidth()

Return type:

float

themeName(self) str[source]

Returns the name of the theme currently linked to the legend.

This usually equates to the theme rendered in the linkedMap().

Added in version 3.14.

Return type:

str

title(self) str[source]

Returns the legend title.

See also

setTitle()

Return type:

str

titleAlignment(self) Qt.AlignmentFlag[source]

Returns the alignment of the legend title.

Return type:

Qt.AlignmentFlag

updateFilterByMap(self, redraw: bool = True)[source]

Updates the legend content when filtered by map.

Parameters:

redraw (bool = True)

updateLegend(self)[source]

Updates the model and all legend entries.

wmsLegendHeight(self) float[source]

Returns the WMS legend height.

Return type:

float

wmsLegendWidth(self) float[source]

Returns the WMS legend width.

Return type:

float

wrapString(self) str[source]

Returns the legend text wrapping string.

See also

setWrapString()

Return type:

str