Class: QgsLayerTreeLayer

Layer tree node points to a map layer.

The node can exist also without a valid instance of a layer (just ID). That means the referenced layer does not need to be loaded in order to use it in layer tree. In such case, resolveReferences() method can be called once the layer is loaded.

A map layer is supposed to be present in one layer tree just once. It is however possible that temporarily a layer exists in one tree more than just once, e.g. while reordering items with drag and drop.

Class Hierarchy

Inheritance diagram of qgis.core.QgsLayerTreeLayer

Base classes

QgsLayerTreeNode

Base class for nodes in a layer tree.

QObject

class qgis.core.QgsLayerTreeLayer[source]

Bases: QgsLayerTreeNode

__init__(layer: QgsMapLayer | None)
Parameters:

layer (Optional[QgsMapLayer])

__init__(layerId: str | None, name: str | None = '', source: str | None = '', provider: str | None = '')

Constructor for QgsLayerTreeLayer using weak references to layer ID, name, public source, and provider key.

Parameters:
  • layerId (Optional[str])

  • name (Optional[str] = '')

  • source (Optional[str] = '')

  • provider (Optional[str] = '')

AllowSplittingLegendNodesOverMultipleColumns = 1
class LegendNodesSplitBehavior

Bases: int

PreventSplittingLegendNodesOverMultipleColumns = 2
UseDefaultLegendSetting = 0
attachToLayer(self)[source]
labelExpression(self) str[source]

Returns the expression member of the LayerTreeNode

Added in version 3.10.

Return type:

str

layer(self) QgsMapLayer | None[source]

Returns the map layer associated with this node.

Warning

This can be (and often is!) None, e.g. in the case of a layer node representing a layer which has not yet been fully loaded into a project, or a layer node representing a layer with an invalid data source. The returned pointer must ALWAYS be checked to avoid dereferencing None.

See also

layerId()

Return type:

Optional[QgsMapLayer]

layerId(self) str[source]

Returns the ID for the map layer associated with this node.

See also

layer()

Return type:

str

signal layerLoaded[source]

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.

signal layerWillBeUnloaded[source]

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.

legendSplitBehavior(self) QgsLayerTreeLayer.LegendNodesSplitBehavior[source]

Returns the column split behavior for the node.

This value controls how legend nodes belonging the to layer may be split over multiple columns in legends.

Added in version 3.14.

Return type:

QgsLayerTreeLayer.LegendNodesSplitBehavior

abstract name(self) str[source]

Returns the layer’s name.

See also

setName()

Return type:

str

patchShape(self) QgsLegendPatchShape[source]

Returns the symbol patch shape to use when rendering the legend node symbol.

See also

setPatchShape()

Added in version 3.14.

Return type:

QgsLegendPatchShape

patchSize(self) QSizeF[source]

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.

See also

setPatchSize()

Added in version 3.14.

Return type:

QSizeF

static readXml(element: QDomElement, context: QgsReadWriteContext) QgsLayerTreeLayer | None[source]

Read layer node from XML. Returns new instance. Does not resolve textual references to layers. Call resolveReferences() afterwards to do it.

readXml(element: QDomElement, project: Optional[QgsProject], context: QgsReadWriteContext) -> Optional[QgsLayerTreeLayer] Read layer node from XML. Returns new instance. Also resolves textual references to layers from the project (calls resolveReferences() internally).

Parameters:
Return type:

Optional[QgsLayerTreeLayer]

abstract resolveReferences(self, project: QgsProject | None, looseMatching: bool = False)[source]

Resolves reference to layer from stored layer ID (if it has not been resolved already)

Parameters:
  • project (Optional[QgsProject])

  • looseMatching (bool = False)

setLabelExpression(self, expression: str | None)[source]

set the expression to evaluate

Added in version 3.10.

Parameters:

expression (Optional[str])

setLegendSplitBehavior(self, behavior: QgsLayerTreeLayer.LegendNodesSplitBehavior)[source]

Sets the column split behavior for the node.

This value controls how legend nodes belonging the to layer may be split over multiple columns in legends.

Added in version 3.14.

Parameters:

behavior (QgsLayerTreeLayer.LegendNodesSplitBehavior)

abstract setName(self, n: str | None)[source]

Sets the layer’s name.

See also

name()

Parameters:

n (Optional[str])

setPatchShape(self, shape: QgsLegendPatchShape)[source]

Sets the symbol patch shape to use when rendering the legend node symbol.

See also

patchShape()

Added in version 3.14.

Parameters:

shape (QgsLegendPatchShape)

setPatchSize(self, size: QSizeF)[source]

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

patchSize()

Added in version 3.14.

Parameters:

size (QSizeF)

setUseLayerName(self, use: bool = True)[source]

Uses the layer’s name if use is True, or the name manually set if False.

Added in version 3.8.

Parameters:

use (bool = True)

useLayerName(self) bool[source]

Returns whether the layer’s name is used, or the name manually set.

Added in version 3.8.

Return type:

bool