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

Abstract Methods

name

Returns the layer's name.

resolveReferences

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

setName

Sets the layer's name.

Methods

attachToLayer

labelExpression

Returns the expression member of the LayerTreeNode

layer

Returns the map layer associated with this node.

layerId

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

legendSplitBehavior

Returns the column split behavior for the node.

patchShape

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

patchSize

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

setLabelExpression

set the expression to evaluate

setLegendSplitBehavior

Sets the column split behavior for the node.

setPatchShape

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

setPatchSize

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

setUseLayerName

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

useLayerName

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

Static Methods

readXml

Read layer node from XML.

Signals

layerLoaded

Emitted when a previously unavailable layer got loaded.

layerWillBeUnloaded

Emitted when a previously available layer got unloaded (from layer registry).

Attributes

AllowSplittingLegendNodesOverMultipleColumns

PreventSplittingLegendNodesOverMultipleColumns

UseDefaultLegendSetting

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]

Emitted when a previously unavailable layer got loaded.

signal layerWillBeUnloaded[source]

Emitted when a previously available layer got unloaded (from layer registry).

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.

Parameters:
Return type:

Optional[QgsLayerTreeLayer]

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

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