Class: QgsLayerTree

Namespace with helper functions for layer tree operations.

Only generally useful routines should be here. Miscellaneous utility functions for work with the layer tree are in QgsLayerTreeUtils class.

Class Hierarchy

Inheritance diagram of qgis.core.QgsLayerTree

Base classes

QgsLayerTreeGroup

Layer tree group node serves as a container for layers and further groups.

QgsLayerTreeNode

Base class for nodes in a layer tree.

QObject

Methods

clear

Clear any information from this layer tree.

customLayerOrder

The order in which layers will be rendered on the canvas.

hasCustomLayerOrder

Determines if the layer order should be derived from the layer tree or if a custom override order shall be used instead.

layerOrder

The order in which layers will be rendered on the canvas.

readLayerOrderFromXml

Load the layer order from an XML element.

setCustomLayerOrder

The order in which layers will be rendered on the canvas.

setCustomLayerOrderByIds

The order in which layers will be rendered on the canvas.

setHasCustomLayerOrder

Determines if the layer order should be derived from the layer tree or if a custom override order shall be used instead.

Static Methods

isGroup

Check whether the node is a valid group node

isLayer

Check whether the node is a valid layer node

readXml

Load the layer tree from an XML element.

Signals

customLayerOrderChanged

Emitted when the custom layer order has changed.

hasCustomLayerOrderChanged

Emitted when the hasCustomLayerOrder flag changes.

layerOrderChanged

Emitted when the layer order has changed.

class qgis.core.QgsLayerTree[source]

Bases: QgsLayerTreeGroup

__init__()

Create a new empty layer tree

clear(self)[source]

Clear any information from this layer tree.

customLayerOrder(self) List[QgsMapLayer]

The order in which layers will be rendered on the canvas. Will only be used if the property hasCustomLayerOrder is True. If you need the current layer order that is active, prefer using layerOrder().

See also

layerOrder()

Return type:

List[QgsMapLayer]

signal customLayerOrderChanged[source]

Emitted when the custom layer order has changed.

hasCustomLayerOrder(self) bool[source]

Determines if the layer order should be derived from the layer tree or if a custom override order shall be used instead.

Return type:

bool

signal hasCustomLayerOrderChanged(hasCustomLayerOrder: bool)[source]

Emitted when the hasCustomLayerOrder flag changes.

Parameters:

hasCustomLayerOrder (bool)

static isGroup(node: QgsLayerTreeNode | None) bool[source]

Check whether the node is a valid group node

Parameters:

node (Optional[QgsLayerTreeNode])

Return type:

bool

static isLayer(node: QgsLayerTreeNode | None) bool[source]

Check whether the node is a valid layer node

Parameters:

node (Optional[QgsLayerTreeNode])

Return type:

bool

layerOrder(self) List[QgsMapLayer]

The order in which layers will be rendered on the canvas. Depending on hasCustomLayerOrder, this will return either the override customLayerOrder or the layer order derived from the tree. This property is read only.

Return type:

List[QgsMapLayer]

signal layerOrderChanged[source]

Emitted when the layer order has changed.

readLayerOrderFromXml(self, doc: QDomElement)[source]

Load the layer order from an XML element. Make sure that this is only called after the layers are loaded.

Parameters:

doc (QDomElement)

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

Load the layer tree from an XML element. It is not required that layers are loaded at this point. resolveReferences() needs to be called after loading the layers and before using the tree.

Parameters:
Return type:

Optional[QgsLayerTree]

setCustomLayerOrder(self, customLayerOrder: Iterable[QgsMapLayer])[source]

The order in which layers will be rendered on the canvas. Will only be used if the property hasCustomLayerOrder is True. If you need the current layer order that is active, prefer using layerOrder().

See also

layerOrder()

Parameters:

customLayerOrder (Iterable[QgsMapLayer])

setCustomLayerOrderByIds(self, customLayerOrder: Iterable[str | None])

The order in which layers will be rendered on the canvas. Will only be used if the property hasCustomLayerOrder is True. If you need the current layer order that is active, prefer using layerOrder().

See also

layerOrder()

Parameters:

customLayerOrder (Iterable[Optional[str]])

setHasCustomLayerOrder(self, hasCustomLayerOrder: bool)[source]

Determines if the layer order should be derived from the layer tree or if a custom override order shall be used instead.

Parameters:

hasCustomLayerOrder (bool)