Class: QgsLayerTreeModelLegendNode

class qgis.core.QgsLayerTreeModelLegendNode

Bases: PyQt5.QtCore.QObject

The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegend implementation.

The objects are used in QgsLayerTreeModel. Custom implementations may offer additional interactivity and customized look.

QgsLayerTreeModelLegendNode(nodeL: Optional[QgsLayerTreeLayer], parent: Optional[QObject] = None) Construct the node with pointer to its parent layer node

Enums

CustomRole

Bases: enum.IntEnum

LegendNodeRoles

alias of CustomRole

Methods

checkAllItems

Checks all checkable items belonging to the same layer as this node.

childEvent

columnBreak

Returns whether a forced column break should occur before the node.

connectNotify

createTemporaryRenderContext

Returns a temporary context or None if legendMapViewData are not valid

customEvent

data

Returns data associated with the item.

disconnectNotify

draw

Entry point called from QgsLegendRenderer to do the rendering.

drawSymbol

Draws symbol on the left side of the item

drawSymbolText

Draws label on the right side of the item

exportSymbolToJson

Adds a symbol in base64 string within a JSON object with the key "icon".

exportToJson

Entry point called from QgsLegendRenderer to do the rendering in a JSON object.

flags

Returns item flags associated with the item.

invalidateMapBasedData

Notification from model that information from associated map view has changed.

isEmbeddedInParent

rtype:

bool

isScaleOK

param scale:

isSignalConnected

layerNode

Returns pointer to the parent layer node

model

Returns pointer to model owning this legend node

receivers

sender

senderSignalIndex

setColumnBreak

Sets whether a forced column break should occur before the node.

setData

Sets some data associated with the item.

setEmbeddedInParent

param embedded:

setUserLabel

param userLabel:

setUserPatchSize

Sets the user (overridden) size for the legend node.

timerEvent

toggleAllItems

Toggle all checkable items belonging to the same layer as this node.

uncheckAllItems

Unchecks all checkable items belonging to the same layer as this node.

userLabel

rtype:

str

userPatchSize

Returns the user (overridden) size for the legend node.

Signals

dataChanged

pyqtSignal(*types, name: str = ..., revision: int = ..., arguments: Sequence = ...) -> PYQT_SIGNAL

sizeChanged

pyqtSignal(*types, name: str = ..., revision: int = ..., arguments: Sequence = ...) -> PYQT_SIGNAL

Attributes

ColorRampLegend

DataDefinedSizeLegend

EmbeddedWidget

ImageLegend

RasterSymbolLegend

SimpleLegend

SymbolLegend

WmsLegend

ColorRampLegend = 7
class CustomRole(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: enum.IntEnum

Legend node data roles

Note

Prior to QGIS 3.36 this was available as QgsLayerTreeModelLegendNode.LegendNodeRoles

Added in version 3.36.

  • RuleKeyRole: Rule key of the node (QString)

  • ParentRuleKeyRole: Rule key of the parent legend node - for legends with tree hierarchy (QString). Added in 2.8

  • NodeTypeRole: Type of node. Added in 3.16

baseClass

alias of QgsLayerTreeModelLegendNode

DataDefinedSizeLegend = 5
EmbeddedWidget = 6
ImageLegend = 3
class ItemContext
class ItemContext(a0: QgsLayerTreeModelLegendNode.ItemContext)

Bases: sip.wrapper

columnLeft
columnRight
context
labelXOffset
maxSiblingSymbolWidth
painter
patchShape
patchSize
point
screenProperties
textDocument
textDocumentMetrics
top
class ItemMetrics
class ItemMetrics(a0: QgsLayerTreeModelLegendNode.ItemMetrics)

Bases: sip.wrapper

labelSize
symbolSize
LegendNodeRoles

alias of CustomRole

class NodeTypes

Bases: int

RasterSymbolLegend = 2
SimpleLegend = 0
SymbolLegend = 1
WmsLegend = 4
checkAllItems(self)

Checks all checkable items belonging to the same layer as this node.

See also

toggleAllItems()

Added in version 3.18.

childEvent(self, a0: QChildEvent | None)
columnBreak(self) bool

Returns whether a forced column break should occur before the node.

See also

setColumnBreak()

Added in version 3.14.

Return type:

bool

connectNotify(self, signal: QMetaMethod)
createTemporaryRenderContext(self) QgsRenderContext | None

Returns a temporary context or None if legendMapViewData are not valid

Return type:

Optional[QgsRenderContext]

customEvent(self, a0: QEvent | None)
data(self, role: int) Any

Returns data associated with the item. Must be implemented in derived class.

Parameters:

role (int)

Return type:

Any

dataChanged

pyqtSignal(*types, name: str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

Parameters:
  • name (str = ...)

  • revision (int = ...)

  • arguments (Sequence = ...)

Return type:

PYQT_SIGNAL

disconnectNotify(self, signal: QMetaMethod)
draw(self, settings: QgsLegendSettings, ctx: QgsLayerTreeModelLegendNode.ItemContext | None) QgsLayerTreeModelLegendNode.ItemMetrics

Entry point called from QgsLegendRenderer to do the rendering. Default implementation calls drawSymbol() and drawSymbolText() methods.

If ctx is None, this is just first stage when preparing layout - without actual rendering.

Parameters:
Return type:

QgsLayerTreeModelLegendNode.ItemMetrics

drawSymbol(self, settings: QgsLegendSettings, ctx: QgsLayerTreeModelLegendNode.ItemContext | None, itemHeight: float) QSizeF

Draws symbol on the left side of the item

Parameters:
  • settings (QgsLegendSettings) – Legend layout configuration

  • ctx (Optional[QgsLayerTreeModelLegendNode.ItemContext]) – Context for rendering - may be None if only doing layout without actual rendering

  • itemHeight (float) – Minimal height of the legend item - used for correct positioning when rendering

Return type:

QSizeF

Returns:

Real size of the symbol (may be bigger than “normal” symbol size from settings)

drawSymbolText(self, settings: QgsLegendSettings, ctx: QgsLayerTreeModelLegendNode.ItemContext | None, symbolSize: QSizeF) QSizeF

Draws label on the right side of the item

Parameters:
  • settings (QgsLegendSettings) – Legend layout configuration

  • ctx (Optional[QgsLayerTreeModelLegendNode.ItemContext]) – Context for rendering - may be None if only doing layout without actual rendering

  • symbolSize (QSizeF) – Real size of the associated symbol - used for correct positioning when rendering

Return type:

QSizeF

Returns:

Size of the label (may span multiple lines)

exportSymbolToJson(self, settings: QgsLegendSettings, context: QgsRenderContext) Dict[str, QJsonValue]

Adds a symbol in base64 string within a JSON object with the key “icon”.

Parameters:

Added in version 3.8.

Return type:

Dict[str, QJsonValue]

exportToJson(self, settings: QgsLegendSettings, context: QgsRenderContext) Dict[str, QJsonValue]

Entry point called from QgsLegendRenderer to do the rendering in a JSON object.

Parameters:

Added in version 3.8.

Return type:

Dict[str, QJsonValue]

flags(self) Qt.ItemFlags

Returns item flags associated with the item. Default implementation returns Qt.ItemIsEnabled.

Return type:

Qt.ItemFlags

invalidateMapBasedData(self)

Notification from model that information from associated map view has changed. Default implementation does nothing.

isEmbeddedInParent(self) bool
Return type:

bool

isScaleOK(self, scale: float) bool
Parameters:

scale (float)

Return type:

bool

isSignalConnected(self, signal: QMetaMethod) bool
layerNode(self) QgsLayerTreeLayer | None

Returns pointer to the parent layer node

Return type:

Optional[QgsLayerTreeLayer]

model(self) QgsLayerTreeModel | None

Returns pointer to model owning this legend node

Return type:

Optional[QgsLayerTreeModel]

receivers(self, signal: PYQT_SIGNAL) int
sender(self) QObject | None
senderSignalIndex(self) int
setColumnBreak(self, breakBeforeNode: bool)

Sets whether a forced column break should occur before the node.

See also

columnBreak()

Added in version 3.14.

Parameters:

breakBeforeNode (bool)

setData(self, value: Any, role: int) bool

Sets some data associated with the item. Default implementation does nothing and returns False.

Parameters:
  • value (Any)

  • role (int)

Return type:

bool

setEmbeddedInParent(self, embedded: bool)
Parameters:

embedded (bool)

setUserLabel(self, userLabel: str | None)
Parameters:

userLabel (Optional[str])

setUserPatchSize(self, size: QSizeF)

Sets the user (overridden) size for the legend node.

If either the width or height are non-zero, they will be used when rendering the legend node instead of the default symbol width or height from QgsLegendSettings.

See also

userPatchSize()

Added in version 3.14.

Parameters:

size (QSizeF)

sizeChanged

pyqtSignal(*types, name: str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

Parameters:
  • name (str = ...)

  • revision (int = ...)

  • arguments (Sequence = ...)

Return type:

PYQT_SIGNAL

timerEvent(self, a0: QTimerEvent | None)
toggleAllItems(self)

Toggle all checkable items belonging to the same layer as this node.

See also

checkAllItems()

Added in version 3.18.

uncheckAllItems(self)

Unchecks all checkable items belonging to the same layer as this node.

See also

checkAllItems()

See also

toggleAllItems()

Added in version 3.18.

userLabel(self) str
Return type:

str

userPatchSize(self) QSizeF

Returns the user (overridden) size for the legend node.

If either the width or height are non-zero, they will be used when rendering the legend node instead of the default symbol width or height from QgsLegendSettings.

Added in version 3.14.

Return type:

QSizeF