Class: QgsLayerTreeLayer

class qgis.core.QgsLayerTreeLayer(layer: QgsMapLayer)

Bases: QgsLayerTreeNode

QgsLayerTreeLayer(layerId: str, name: str = ‘’, source: str = ‘’, provider: str = ‘’) Constructor for QgsLayerTreeLayer using weak references to layer ID, name, public source, and provider key.

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.

New in version 2.4:

Methods

attachToLayer

childEvent

clone

rtype

QgsLayerTreeLayer

connectNotify

customEvent

disconnectNotify

dump

rtype

str

insertChildrenPrivate

Low-level insertion of children to the node.

isSignalConnected

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.

name

Returns the layer's name.

patchShape

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

patchSize

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

readCommonXml

Read common XML elements.

readXml

Read layer node from XML.

receivers

removeChildrenPrivate

Low-level removal of children from the node.

resolveReferences

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

sender

senderSignalIndex

setLabelExpression

set the expression to evaluate

setLegendSplitBehavior

Sets the column split behavior for the node.

setName

Sets the layer's name.

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.

timerEvent

useLayerName

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

writeCommonXml

Write common XML elements.

writeXml

param parentElement

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

AllowSplittingLegendNodesOverMultipleColumns = 1
class LegendNodesSplitBehavior

Bases: int

PreventSplittingLegendNodesOverMultipleColumns = 2
UseDefaultLegendSetting = 0
attachToLayer(self)
childEvent(self, QChildEvent)
clone(self) QgsLayerTreeLayer
Return type

QgsLayerTreeLayer

connectNotify(self, QMetaMethod)
customEvent(self, QEvent)
disconnectNotify(self, QMetaMethod)
dump(self) str
Return type

str

insertChildrenPrivate(self, index: int, nodes: Iterable[QgsLayerTreeNode])

Low-level insertion of children to the node. The children must not have any parent yet!

isSignalConnected(self, QMetaMethod) bool
labelExpression(self) str

Returns the expression member of the LayerTreeNode

New in version 3.10.

Return type

str

layer(self) QgsMapLayer

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

QgsMapLayer

layerId(self) str

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

See also

layer()

Return type

str

layerLoaded

Emitted when a previously unavailable layer got loaded. [signal]

layerWillBeUnloaded

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

New in version 2.6: [signal]

legendSplitBehavior(self) QgsLayerTreeLayer.LegendNodesSplitBehavior

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.

New in version 3.14.

Return type

QgsLayerTreeLayer.LegendNodesSplitBehavior

name(self) str

Returns the layer’s name.

See also

setName()

New in version 3.0.

Return type

str

patchShape(self) QgsLegendPatchShape

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

See also

setPatchShape()

New in version 3.14.

Return type

QgsLegendPatchShape

patchSize(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.

See also

setPatchSize()

New in version 3.14.

Return type

QSizeF

readCommonXml(self, element: QDomElement)

Read common XML elements.

readXml(element: QDomElement, context: QgsReadWriteContext) QgsLayerTreeLayer

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: QgsProject, context: QgsReadWriteContext) -> QgsLayerTreeLayer Read layer node from XML. Returns new instance. Also resolves textual references to layers from the project (calls resolveReferences() internally).

New in version 3.0.

Parameters
Return type

QgsLayerTreeLayer

receivers(self, PYQT_SIGNAL) int
removeChildrenPrivate(self, from_: int, count: int, destroy: bool = True)

Low-level removal of children from the node.

resolveReferences(self, project: QgsProject, looseMatching: bool = False)

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

New in version 3.0.

Parameters
  • project (QgsProject) –

  • looseMatching (bool = False) –

sender(self) QObject
senderSignalIndex(self) int
setLabelExpression(self, expression: str)

set the expression to evaluate

New in version 3.10.

Parameters

expression (str) –

setLegendSplitBehavior(self, behavior: QgsLayerTreeLayer.LegendNodesSplitBehavior)

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.

New in version 3.14.

Parameters

behavior (QgsLayerTreeLayer.LegendNodesSplitBehavior) –

setName(self, n: str)

Sets the layer’s name.

See also

name()

New in version 3.0.

Parameters

n (str) –

setPatchShape(self, shape: QgsLegendPatchShape)

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

See also

patchShape()

New in version 3.14.

Parameters

shape (QgsLegendPatchShape) –

setPatchSize(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

patchSize()

New in version 3.14.

Parameters

size (QSizeF) –

setUseLayerName(self, use: bool = True)

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

New in version 3.8.

Parameters

use (bool = True) –

timerEvent(self, QTimerEvent)
useLayerName(self) bool

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

New in version 3.8.

Return type

bool

writeCommonXml(self, element: QDomElement)

Write common XML elements.

writeXml(self, parentElement: QDomElement, context: QgsReadWriteContext)
Parameters