Class: QgsLayoutItemLegend¶
- class qgis.core.QgsLayoutItemLegend¶
Bases:
QgsLayoutItem
A layout item subclass for map legends.
New in version 3.0.
QgsLayoutItemLegend(layout:
QgsLayout
) Constructor forQgsLayoutItemLegend
, with the specified parentlayout
.Methods
- param visitor:
Sets the legend's item bounds to fit the whole legend content.
Adjusts the specified
point
at which areference
position of the item sits and returns the top left corner of the item, if reference point were placed at the specified position.Applies any present data defined size overrides to the specified layout
size
.Applies any item-specific size constraint handling to a given
targetSize
in layout units.Returns whether the legend content should auto update to reflect changes in the project's layer tree.
Returns the legend box space.
Returns the legend column count.
Returns the legend column spacing.
Returns a new legend item for the specified
layout
.- rtype:
QgsExpressionContext
- rtype:
str
- param context:
Draws the background for the item.
Draws a debugging rectangle of the item's current bounds within the specified painter.
Draws the frame around the item.
Returns whether a stroke will be drawn around raster symbol items.
Draws a "refreshing" overlay icon on the item.
Returns whether column widths should be equalized.
- rtype:
QgsLayoutItem.ExportLayerBehavior
Returns the maps to use when filtering legend content by map extents.
Returns the legend font color.
Returns the path to use when drawing the item's frame or background.
- rtype:
QIcon
- rtype:
bool
- rtype:
QgsLayoutItem.Flags
Find out whether legend items are filtered to show just the ones visible in the associated map
Returns whether to filter out legend elements outside of the current atlas feature.
Returns the legend's renderer settings object.
Returns the spacing in-between lines in layout units.
Returns the associated map.
Returns the maximum symbol size (in mm).
Returns the minimum symbol size (in mm).
Returns the legend model.
- param painter:
Returns the current position (in layout units) of a
reference
point for the item.Returns the stroke color for the stroke drawn around raster symbol items.
Returns the stroke width (in layout units) for the stroke drawn around raster symbol items.
Sets object properties from a DOM element
- param element:
Refresh item's background color, considering data defined colors.
Refresh item's blend mode, considering data defined blend mode.
- param property:
Refresh item's frame, considering data defined colors and frame size.
Refreshes an item's position by rechecking it against any possible overrides such as data defined positioning.
Refreshes an item's rotation by rechecking it against any possible overrides such as data defined rotation.
Refreshes an item's size by rechecking it against any possible item fixed or minimum sizes.
Refresh item's opacity, considering data defined opacity.
Returns whether the legend should automatically resize to fit its contents.
Returns reference to modifiable legend style.
Sets whether the legend content should auto update to reflect changes in the project's layer tree.
Sets the legend box
space
.Sets the legend column
count
.Sets the legend column
spacing
.Sets whether a stroke will be drawn around raster symbol items.
Sets whether column widths should be equalized.
Sets the
maps
to use when filtering legend content by map extents.Sets a fixed
size
for the layout item, which prevents it from being freely resized.Sets the legend font
color
.Set whether legend items should be filtered to show just the ones visible in the associated map.
When set to
True
, during an atlas rendering, it will filter out legend elements where features are outside the current atlas feature.Sets the
spacing
in-between multiple lines.Sets the
map
to associate with the legend.Set the maximum symbol
size
for symbol (in millimeters).Sets the minimum allowed
size
for the layout item.Set the minimum symbol
size
for symbol (in millimeters).Sets the stroke
color
for the stroke drawn around raster symbol items.Sets the stroke width for the stroke drawn around raster symbol items.
Sets whether the legend should automatically resize to fit its contents.
Sets whether the legend items from a single layer can be split over multiple columns.
Sets the style of
component
tostyle
for the legend.Sets the style
font
for a legendcomponent
.Set the
margin
for a legendcomponent
.Sets the
alignment
for placement of legend symbols.Sets the legend symbol
height
.Sets the legend symbol
width
.Sets the legend
title
.Sets the
alignment
of the legend title.Sets the WMS legend
height
.Sets the WMS legend
width
.Sets the legend text wrapping
string
.Returns whether the legend items from a single layer can be split over multiple columns.
Returns legend style.
Returns the font settings for a legend
component
.Returns the alignment for placement of legend symbols.
Returns the legend symbol height.
Returns the legend symbol width.
Returns the name of the theme currently linked to the legend.
Returns the legend title.
Returns the alignment of the legend title.
Returns the position for the reference point of the item, if the top-left of the item was placed at the specified
point
.- rtype:
int
Updates the legend content when filtered by map.
Updates the model and all legend entries.
Returns the WMS legend height.
Returns the WMS legend width.
Returns the legend text wrapping string.
Stores object properties within an XML DOM element.
- param element:
- accept(self, visitor: QgsStyleEntityVisitorInterface) bool ¶
- Parameters:
visitor (QgsStyleEntityVisitorInterface) –
- Return type:
bool
- adjustBoxSize(self)¶
Sets the legend’s item bounds to fit the whole legend content.
- adjustPointForReferencePosition(self, point: QPointF | QPoint, size: QSizeF, reference: QgsLayoutItem.ReferencePoint) QPointF ¶
Adjusts the specified
point
at which areference
position of the item sits and returns the top left corner of the item, if reference point were placed at the specified position.
- applyDataDefinedSize(self, size: QgsLayoutSize) QgsLayoutSize ¶
Applies any present data defined size overrides to the specified layout
size
.
- applyItemSizeConstraint(self, targetSize: QSizeF) QSizeF ¶
Applies any item-specific size constraint handling to a given
targetSize
in layout units. Subclasses can override this method if they need to apply advanced logic regarding item sizes, which cannot be covered bysetFixedSize()
orsetMinimumSize()
. Item size constraints are applied after fixed, minimum and data defined size constraints.See also
See also
- autoUpdateModel(self) bool ¶
Returns whether the legend content should auto update to reflect changes in the project’s layer tree.
See also
- Return type:
bool
- boxSpace(self) float ¶
Returns the legend box space.
See also
- Return type:
float
- childEvent(self, QChildEvent)¶
- columnCount(self) int ¶
Returns the legend column count.
See also
- Return type:
int
- columnSpace(self) float ¶
Returns the legend column spacing.
See also
- Return type:
float
- connectNotify(self, QMetaMethod)¶
- contextMenuEvent(self, QGraphicsSceneContextMenuEvent)¶
- create(layout: QgsLayout) QgsLayoutItemLegend ¶
Returns a new legend item for the specified
layout
.The caller takes responsibility for deleting the returned object.
- Parameters:
layout (QgsLayout) –
- Return type:
- createExpressionContext(self) QgsExpressionContext ¶
- Return type:
- customEvent(self, QEvent)¶
- disconnectNotify(self, QMetaMethod)¶
- displayName(self) str ¶
- Return type:
str
- dragEnterEvent(self, QGraphicsSceneDragDropEvent)¶
- dragLeaveEvent(self, QGraphicsSceneDragDropEvent)¶
- dragMoveEvent(self, QGraphicsSceneDragDropEvent)¶
- draw(self, context: QgsLayoutItemRenderContext)¶
- Parameters:
context (QgsLayoutItemRenderContext) –
- drawBackground(self, context: QgsRenderContext)¶
Draws the background for the item.
See also
- drawDebugRect(self, painter: QPainter)¶
Draws a debugging rectangle of the item’s current bounds within the specified painter.
- Parameters:
painter – destination QPainter
- drawFrame(self, context: QgsRenderContext)¶
Draws the frame around the item.
See also
- drawRasterStroke(self) bool ¶
Returns whether a stroke will be drawn around raster symbol items.
See also
See also
See also
- Return type:
bool
- drawRefreshingOverlay(self, painter: QPainter, itemStyle: QStyleOptionGraphicsItem)¶
Draws a “refreshing” overlay icon on the item.
New in version 3.32.
- dropEvent(self, QGraphicsSceneDragDropEvent)¶
- equalColumnWidth(self) bool ¶
Returns whether column widths should be equalized.
See also
- Return type:
bool
- exportLayerBehavior(self) QgsLayoutItem.ExportLayerBehavior ¶
- Return type:
- filterByMapItems(self) List[QgsLayoutItemMap] ¶
Returns the maps to use when filtering legend content by map extents.
See also
See also
New in version 3.32.
- Return type:
List[QgsLayoutItemMap]
- finalizeRestoreFromXml(self)¶
- focusInEvent(self, QFocusEvent)¶
- focusOutEvent(self, QFocusEvent)¶
- fontColor(self) QColor ¶
Returns the legend font color.
See also
Deprecated since version use:
QgsLegendStyle.setTextFormat()
fromstyle()
instead.- Return type:
QColor
- framePath(self) QPainterPath ¶
Returns the path to use when drawing the item’s frame or background.
See also
See also
New in version 3.16.
- hoverEnterEvent(self, QGraphicsSceneHoverEvent)¶
- hoverLeaveEvent(self, QGraphicsSceneHoverEvent)¶
- hoverMoveEvent(self, QGraphicsSceneHoverEvent)¶
- icon(self) QIcon ¶
- Return type:
QIcon
- inputMethodEvent(self, QInputMethodEvent)¶
- inputMethodQuery(self, Qt.InputMethodQuery) Any ¶
- invalidateCache(self)¶
- isRefreshing(self) bool ¶
- Return type:
bool
- isSignalConnected(self, QMetaMethod) bool ¶
- itemChange(self, QGraphicsItem.GraphicsItemChange, Any) Any ¶
- itemFlags(self) QgsLayoutItem.Flags ¶
- Return type:
- keyPressEvent(self, QKeyEvent)¶
- keyReleaseEvent(self, QKeyEvent)¶
- legendFilterByMapEnabled(self) bool ¶
Find out whether legend items are filtered to show just the ones visible in the associated map
See also
- Return type:
bool
- legendFilterOutAtlas(self) bool ¶
Returns whether to filter out legend elements outside of the current atlas feature.
See also
- Return type:
bool
- legendSettings(self) QgsLegendSettings ¶
Returns the legend’s renderer settings object.
- Return type:
- lineSpacing(self) float ¶
Returns the spacing in-between lines in layout units.
See also
Deprecated since version use:
QgsLegendStyle.textFormat()
fromstyle()
instead.- Return type:
float
- linkedMap(self) QgsLayoutItemMap ¶
Returns the associated map.
See also
- Return type:
- maximumSymbolSize(self) float ¶
Returns the maximum symbol size (in mm). 0.0 means there is no maximum set.
See also
New in version 3.16.
- Return type:
float
- minimumSymbolSize(self) float ¶
Returns the minimum symbol size (in mm). A value 0.0 means there is no minimum set.
See also
New in version 3.16.
- Return type:
float
- model(self) QgsLegendModel ¶
Returns the legend model.
- Return type:
- mouseDoubleClickEvent(self, QGraphicsSceneMouseEvent)¶
- mouseMoveEvent(self, QGraphicsSceneMouseEvent)¶
- mousePressEvent(self, QGraphicsSceneMouseEvent)¶
- mouseReleaseEvent(self, QGraphicsSceneMouseEvent)¶
- paint(self, painter: QPainter, itemStyle: QStyleOptionGraphicsItem, pWidget: QWidget)¶
- Parameters:
painter (QPainter) –
itemStyle (QStyleOptionGraphicsItem) –
pWidget (QWidget) –
- positionAtReferencePoint(self, reference: QgsLayoutItem.ReferencePoint) QPointF ¶
Returns the current position (in layout units) of a
reference
point for the item.
- prepareGeometryChange(self)¶
- rasterStrokeColor(self) QColor ¶
Returns the stroke color for the stroke drawn around raster symbol items. The stroke is only drawn if
drawRasterStroke()
isTrue
.See also
See also
See also
- Return type:
QColor
- rasterStrokeWidth(self) float ¶
Returns the stroke width (in layout units) for the stroke drawn around raster symbol items. The stroke is only drawn if
drawRasterStroke()
isTrue
.See also
See also
See also
- Return type:
float
- readObjectPropertiesFromElement(self, parentElement: QDomElement, document: QDomDocument, context: QgsReadWriteContext) bool ¶
Sets object properties from a DOM element
- Parameters:
parentElement – is the parent DOM element for the object
document – DOM document
context – read write context
- Returns:
True
if read was successful
See also
- readPropertiesFromElement(self, element: QDomElement, document: QDomDocument, context: QgsReadWriteContext) bool ¶
- Parameters:
element (QDomElement) –
document (QDomDocument) –
context (QgsReadWriteContext) –
- Return type:
bool
- receivers(self, PYQT_SIGNAL) int ¶
- refresh(self)¶
- refreshBackgroundColor(self, updateItem: bool = True)¶
Refresh item’s background color, considering data defined colors. If
updateItem
is set toFalse
, the item will not be automatically updated after the frame color is set and a later call toupdate()
must be made.
- refreshBlendMode(self)¶
Refresh item’s blend mode, considering data defined blend mode.
- refreshDataDefinedProperty(self, property: QgsLayoutObject.DataDefinedProperty = QgsLayoutObject.AllProperties)¶
- Parameters:
property (QgsLayoutObject.DataDefinedProperty = QgsLayoutObject.AllProperties) –
- refreshFrame(self, updateItem: bool = True)¶
Refresh item’s frame, considering data defined colors and frame size. If
updateItem
is set toFalse
, the item will not be automatically updated after the frame is set and a later call toupdate()
must be made.
- refreshItemPosition(self)¶
Refreshes an item’s position by rechecking it against any possible overrides such as data defined positioning.
See also
- refreshItemRotation(self, origin: QPointF | QPoint = None)¶
Refreshes an item’s rotation by rechecking it against any possible overrides such as data defined rotation.
The optional
origin
point specifies the origin (in item coordinates) around which the rotation should be applied.See also
See also
- refreshItemSize(self)¶
Refreshes an item’s size by rechecking it against any possible item fixed or minimum sizes.
See also
See also
See also
- refreshOpacity(self, updateItem: bool = True)¶
Refresh item’s opacity, considering data defined opacity. If
updateItem
is set toFalse
the item will not be automatically updated after the opacity is set and a later call toupdate()
must be made.
- resizeToContents(self) bool ¶
Returns whether the legend should automatically resize to fit its contents.
See also
- Return type:
bool
- rstyle(self, s: QgsLegendStyle.Style) QgsLegendStyle ¶
Returns reference to modifiable legend style.
- Parameters:
s (QgsLegendStyle.Style) –
- Return type:
- sceneEvent(self, QEvent) bool ¶
- sceneEventFilter(self, QGraphicsItem, QEvent) bool ¶
- sender(self) QObject ¶
- senderSignalIndex(self) int ¶
- setAutoUpdateModel(self, autoUpdate: bool)¶
Sets whether the legend content should auto update to reflect changes in the project’s layer tree.
See also
- Parameters:
autoUpdate (bool) –
- setBoxSpace(self, space: float)¶
Sets the legend box
space
.See also
- Parameters:
space (float) –
- setColumnCount(self, count: int)¶
Sets the legend column
count
.See also
- Parameters:
count (int) –
- setColumnSpace(self, spacing: float)¶
Sets the legend column
spacing
.See also
- Parameters:
spacing (float) –
- setDrawRasterStroke(self, enabled: bool)¶
Sets whether a stroke will be drawn around raster symbol items.
- Parameters:
enabled (bool) – set to
True
to draw borders
See also
See also
See also
- setEqualColumnWidth(self, equalize: bool)¶
Sets whether column widths should be equalized.
See also
- Parameters:
equalize (bool) –
- setFilterByMapItems(self, maps: Iterable[QgsLayoutItemMap])¶
Sets the
maps
to use when filtering legend content by map extents.See also
See also
New in version 3.32.
- Parameters:
maps (Iterable[QgsLayoutItemMap]) –
- setFixedSize(self, size: QgsLayoutSize)¶
Sets a fixed
size
for the layout item, which prevents it from being freely resized. Set an empty size if item can be freely resized.See also
fixedSize()
See also
- setFontColor(self, color: QColor | Qt.GlobalColor | QGradient)¶
Sets the legend font
color
.See also
Deprecated since version use:
QgsLegendStyle.setTextFormat()
fromstyle()
instead.- Parameters:
color (Union[QColor) –
- setLegendFilterByMapEnabled(self, enabled: bool)¶
Set whether legend items should be filtered to show just the ones visible in the associated map.
See also
- Parameters:
enabled (bool) –
- setLegendFilterOutAtlas(self, doFilter: bool)¶
When set to
True
, during an atlas rendering, it will filter out legend elements where features are outside the current atlas feature.See also
- Parameters:
doFilter (bool) –
- setLineSpacing(self, spacing: float)¶
Sets the
spacing
in-between multiple lines.See also
Deprecated since version use:
QgsLegendStyle.setTextFormat()
fromstyle()
instead.- Parameters:
spacing (float) –
- setLinkedMap(self, map: QgsLayoutItemMap)¶
Sets the
map
to associate with the legend.See also
See also
- Parameters:
map (QgsLayoutItemMap) –
- setMaximumSymbolSize(self, size: float)¶
Set the maximum symbol
size
for symbol (in millimeters).A symbol size of 0.0 indicates no maximum is set.
See also
New in version 3.16.
- Parameters:
size (float) –
- setMinimumSize(self, size: QgsLayoutSize)¶
Sets the minimum allowed
size
for the layout item. Set an empty size if item can be freely resized.See also
minimumSize()
See also
- setMinimumSymbolSize(self, size: float)¶
Set the minimum symbol
size
for symbol (in millimeters).A symbol size of 0.0 indicates no minimum is set.
See also
New in version 3.16.
- Parameters:
size (float) –
- setRasterStrokeColor(self, color: QColor | Qt.GlobalColor | QGradient)¶
Sets the stroke
color
for the stroke drawn around raster symbol items. The stroke is only drawn ifdrawRasterStroke()
isTrue
.See also
See also
See also
- Parameters:
color (Union[QColor) –
- setRasterStrokeWidth(self, width: float)¶
Sets the stroke width for the stroke drawn around raster symbol items. The stroke is only drawn if
drawRasterStroke()
isTrue
.See also
See also
See also
- Parameters:
width (float) –
- setResizeToContents(self, enabled: bool)¶
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.
See also
- setSplitLayer(self, enabled: bool)¶
Sets whether the legend items from a single layer can be split over multiple columns.
See also
- Parameters:
enabled (bool) –
- setStyle(self, component: QgsLegendStyle.Style, style: QgsLegendStyle)¶
Sets the style of
component
tostyle
for the legend.- Parameters:
component (QgsLegendStyle.Style) –
style (QgsLegendStyle) –
- setStyleFont(self, component: QgsLegendStyle.Style, font: QFont)¶
Sets the style
font
for a legendcomponent
.See also
Deprecated since version use:
QgsLegendStyle.setTextFormat()
fromstyle()
instead.- Parameters:
component (QgsLegendStyle.Style) –
font (QFont) –
- setStyleMargin(self, component: QgsLegendStyle.Style, margin: float)¶
Set the
margin
for a legendcomponent
.setStyleMargin(self, component: QgsLegendStyle.Style, side: QgsLegendStyle.Side, margin: float) Set the
margin
for a particularside
of a legendcomponent
.- Parameters:
component (QgsLegendStyle.Style) –
margin (float) –
- setSymbolAlignment(self, alignment: Qt.AlignmentFlag)¶
Sets the
alignment
for placement of legend symbols.Only Qt.AlignLeft or Qt.AlignRight are supported values.
See also
New in version 3.10.
- Parameters:
alignment (Qt.AlignmentFlag) –
- setSymbolHeight(self, height: float)¶
Sets the legend symbol
height
.See also
- Parameters:
height (float) –
- setSymbolWidth(self, width: float)¶
Sets the legend symbol
width
.See also
- Parameters:
width (float) –
- setTitleAlignment(self, alignment: Qt.AlignmentFlag)¶
Sets the
alignment
of the legend title.See also
- Parameters:
alignment (Qt.AlignmentFlag) –
- setWmsLegendHeight(self, height: float)¶
Sets the WMS legend
height
.See also
- Parameters:
height (float) –
- setWmsLegendWidth(self, width: float)¶
Sets the WMS legend
width
.See also
- Parameters:
width (float) –
- setWrapString(self, string: str)¶
Sets the legend text wrapping
string
.See also
- Parameters:
string (str) –
- splitLayer(self) bool ¶
Returns whether the legend items from a single layer can be split over multiple columns.
See also
- Return type:
bool
- style(self, s: QgsLegendStyle.Style) QgsLegendStyle ¶
Returns legend style.
- Parameters:
s (QgsLegendStyle.Style) –
- Return type:
- styleFont(self, component: QgsLegendStyle.Style) QFont ¶
Returns the font settings for a legend
component
.See also
Deprecated since version use:
QgsLegendStyle.textFormat()
fromstyle()
instead.- Parameters:
component (QgsLegendStyle.Style) –
- Return type:
QFont
- symbolAlignment(self) Qt.AlignmentFlag ¶
Returns the alignment for placement of legend symbols.
Only Qt.AlignLeft or Qt.AlignRight are supported values.
See also
New in version 3.10.
- Return type:
Qt.AlignmentFlag
- symbolHeight(self) float ¶
Returns the legend symbol height.
See also
- Return type:
float
- symbolWidth(self) float ¶
Returns the legend symbol width.
See also
- Return type:
float
- themeName(self) str ¶
Returns the name of the theme currently linked to the legend.
This usually equates to the theme rendered in the
linkedMap()
.New in version 3.14.
- Return type:
str
- timerEvent(self, QTimerEvent)¶
- title(self) str ¶
Returns the legend title.
See also
- Return type:
str
- titleAlignment(self) Qt.AlignmentFlag ¶
Returns the alignment of the legend title.
See also
- Return type:
Qt.AlignmentFlag
- topLeftToReferencePoint(self, point: QgsLayoutPoint) QgsLayoutPoint ¶
Returns the position for the reference point of the item, if the top-left of the item was placed at the specified
point
.
- type(self) int ¶
- Return type:
int
- updateFilterByMap(self, redraw: bool = True)¶
Updates the legend content when filtered by map.
- Parameters:
redraw (bool = True) –
- updateLegend(self)¶
Updates the model and all legend entries.
- updateMicroFocus(self)¶
- wheelEvent(self, QGraphicsSceneWheelEvent)¶
- wmsLegendHeight(self) float ¶
Returns the WMS legend height.
See also
- Return type:
float
- wmsLegendWidth(self) float ¶
Returns the WMS legend width.
See also
- Return type:
float
- wrapString(self) str ¶
Returns the legend text wrapping string.
See also
- Return type:
str
- writeObjectPropertiesToElement(self, parentElement: QDomElement, document: QDomDocument, context: QgsReadWriteContext) bool ¶
Stores object properties within an XML DOM element.
- Parameters:
parentElement – is the parent DOM element to store the object’s properties in
document – DOM document
context – read write context
- Returns:
True
if write was successful
See also
- writePropertiesToElement(self, element: QDomElement, document: QDomDocument, context: QgsReadWriteContext) bool ¶
- Parameters:
element (QDomElement) –
document (QDomDocument) –
context (QgsReadWriteContext) –
- Return type:
bool