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

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]

pyqtSignal(*types, name: str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

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[source]

pyqtSignal(*types, name: str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

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

Check whether the node is a valid custom node

Added in version 4.0.

Parameters:

node (Optional[QgsLayerTreeNode])

Return type:

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

layerAndCustomNodeOrder(self) List[QgsLayerTreeNode]

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

Since custom nodes don’t support custom layer order, this returns the node order derived from the tree.

Depending on the use case, not all custom nodes are to be rendered on the canvas, so callers of this method will probably need another layer of logic to use the returned order in a meaningful way.

This property is read only.

See also

layerOrder()

Added in version 4.0.

Return type:

List[QgsLayerTreeNode]

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]

pyqtSignal(*types, name: str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

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

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)