QgsLayerTreeLayer class

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.

Base classes

class QgsLayerTreeNode
This class is a base class for nodes in a layer tree.

Public static functions

static auto readXml(QDomElement& element, const QgsReadWriteContext& context) -> QgsLayerTreeLayer*
Read layer node from XML.
static auto readXml(QDomElement& element, const QgsProject* project, const QgsReadWriteContext& context) -> QgsLayerTreeLayer*
Read layer node from XML.

Constructors, destructors, conversion operators

QgsLayerTreeLayer(const QString& layerId, const QString& name = QString(), const QString& source = QString(), const QString& provider = QString()) explicit
Constructor for QgsLayerTreeLayer using weak references to layer ID, name, public source, and provider key.

Public functions

auto clone() const -> QgsLayerTreeLayer* override
Create a copy of the node. Returns new instance.
auto dump() const -> QString override
Returns string with layer tree structure. For debug purposes only.
auto layer() const -> QgsMapLayer*
Returns the map layer associated with this node.
auto layerId() const -> QString
Returns the ID for the map layer associated with this node.
auto name() const -> QString override
Returns the layer's name.
void resolveReferences(const QgsProject* project, bool looseMatching = false) override
Resolves reference to layer from stored layer ID (if it has not been resolved already)
void setName(const QString& n) override
Sets the layer's name.
void writeXml(QDomElement& parentElement, const QgsReadWriteContext& context) override
Write layer tree to XML.

Signals

void layerLoaded()
Emitted when a previously unavailable layer got loaded.
void layerWillBeUnloaded()
Emitted when a previously available layer got unloaded (from layer registry).

Protected variables

QString mLayerName
Layer name - only used if layer does not exist.
QgsMapLayerRef mRef
Weak reference to the layer (or just it's ID if the reference is not resolved yet)

Function documentation

static QgsLayerTreeLayer* QgsLayerTreeLayer::readXml(QDomElement& element, const QgsReadWriteContext& context)

Read layer node from XML.

Returns new instance. Does not resolve textual references to layers. Call resolveReferences() afterwards to do it.

static QgsLayerTreeLayer* QgsLayerTreeLayer::readXml(QDomElement& element, const QgsProject* project, const QgsReadWriteContext& context)

Read layer node from XML.

Returns new instance. Also resolves textual references to layers from the project (calls resolveReferences() internally).

QgsMapLayer* QgsLayerTreeLayer::layer() const

Returns the map layer associated with this node.

QString QgsLayerTreeLayer::layerId() const

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

QString QgsLayerTreeLayer::name() const override

Returns the layer's name.

void QgsLayerTreeLayer::resolveReferences(const QgsProject* project, bool looseMatching = false) override

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

void QgsLayerTreeLayer::setName(const QString& n) override

Sets the layer's name.

void QgsLayerTreeLayer::layerWillBeUnloaded() signal

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