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.

Parameters

layer

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]

name(self) → str

Returns the layer’s name.

See also

setName()

New in version 3.0.

Return type

str

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) –

setName(self, n: str)

Sets the layer’s name.

See also

name()

New in version 3.0.

Parameters

n (str) –

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