Class: QgsLayerTreeNode

Base class for nodes in a layer tree.

Layer tree is a hierarchical structure consisting of group and layer nodes:

  • group nodes are containers and may contain children (layer and group nodes)

  • layer nodes point to map layers, they do not contain further children

Layer trees may be used for organization of layers, typically a layer tree is exposed to the user using QgsLayerTreeView widget which shows the tree and allows manipulation with the tree.

Ownership of nodes: every node is owned by its parent. Therefore once node is added to a layer tree, it is the responsibility of the parent to delete it when the node is not needed anymore. Deletion of root node of a tree will delete all nodes of the tree.

Signals: signals are propagated from children to parent. That means it is sufficient to connect to root node in order to get signals about updates in the whole layer tree. When adding or removing a node that contains further children (i.e. a whole subtree), the addition/removal signals are emitted only for the root node of the subtree that is being added or removed.

Custom properties: Every node may have some custom properties assigned to it. This mechanism allows third parties store additional data with the nodes. The properties are used within QGIS code (whether to show layer in overview, whether the node is embedded from another project etc), but may be also used by third party plugins. Custom properties are stored also in the project file. The storage is not efficient for large amount of data.

Custom properties that have already been used within QGIS:

  • “loading” - whether the project is being currently loaded (root node only)

  • “overview” - whether to show a layer in overview

  • “showFeatureCount” - whether to show feature counts in layer tree (vector only)

  • “embedded” - whether the node comes from an external project

  • “embedded_project” - path to the external project (embedded root node only)

  • “legend/…” - properties for legend appearance customization

  • “expandedLegendNodes” - list of layer’s legend nodes’ rules in expanded state

See also

QgsLayerTree

Note

This is an abstract class, with methods which must be implemented by a subclass.

The following methods must be implemented: clone(), dump(), name(), resolveReferences(), setName(), writeXml()

Class Hierarchy

Inheritance diagram of qgis.core.QgsLayerTreeNode

Base classes

QObject

Subclasses

QgsLayerTreeGroup

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

QgsLayerTreeCustomNode

Layer tree custom node serves as a node for objects that are not layers nor groups.

QgsLayerTreeLayer

Layer tree node points to a map layer.

class qgis.core.QgsLayerTreeNode[source]

Bases: QObject

__init__(t: QgsLayerTreeNode.NodeType, checked: bool = True)

Constructor

Parameters:
__init__(other: QgsLayerTreeNode)
Parameters:

other (QgsLayerTreeNode)

NodeCustom = 2
NodeGroup = 0
NodeLayer = 1
class NodeType

Bases: int

signal addedChildren[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.

checkedLayers(self) List[QgsMapLayer]

Returns a list of any checked layers which belong to this node or its children.

Return type:

List[QgsMapLayer]

children(self) List[QgsLayerTreeNode]

Gets list of children of the node. Children are owned by the parent

Return type:

List[QgsLayerTreeNode]

abstract clone(self) QgsLayerTreeNode | None[source]

Create a copy of the node. Returns new instance

Return type:

Optional[QgsLayerTreeNode]

customProperties(self) List[str][source]

Returns list of keys stored in custom properties

Return type:

List[str]

customProperty(self, key: str | None, defaultValue: Any = None) Any[source]

Read a custom property from layer. Properties are stored in a map and saved in project file.

Parameters:
  • key (Optional[str])

  • defaultValue (Any = None)

Return type:

Any

signal customPropertyChanged[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.

depth(self) int[source]

Returns the depth of this node, i.e. the number of its ancestors

Added in version 3.14.

Return type:

int

abstract dump(self) str[source]

Returns string with layer tree structure. For debug purposes only

Return type:

str

signal expandedChanged[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.

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

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

Parameters:
isExpanded(self) bool[source]

Returns whether the node should be shown as expanded or collapsed in GUI

Return type:

bool

isItemVisibilityCheckedRecursive(self) bool[source]

Returns whether this node is checked and all its children.

Return type:

bool

isItemVisibilityUncheckedRecursive(self) bool[source]

Returns whether this node is unchecked and all its children.

Return type:

bool

isVisible(self) bool[source]

Returns whether a node is really visible (ie checked and all its ancestors checked as well)

Return type:

bool

itemVisibilityChecked(self) bool[source]

Returns whether a node is checked (independently of its ancestors or children)

Return type:

bool

abstract name(self) str[source]

Returns name of the node

Return type:

str

signal nameChanged[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.

nodeType(self) QgsLayerTreeNode.NodeType[source]

Find out about type of the node. It is usually shorter to use convenience functions from QgsLayerTree namespace for that

Return type:

QgsLayerTreeNode.NodeType

parent(self) QgsLayerTreeNode | None[source]

Gets pointer to the parent. If parent is None, the node is a root node

Return type:

Optional[QgsLayerTreeNode]

readCommonXml(self, element: QDomElement)[source]

Read common XML elements.

Parameters:

element (QDomElement)

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

Read layer tree from XML. Returns new instance. Does not resolve textual references to layers. Call resolveReferences() afterwards to do it.

readXml(element: QDomElement, project: Optional[QgsProject]) -> Optional[QgsLayerTreeNode] Read layer tree from XML. Returns new instance. Also resolves textual references to layers from the project (calls resolveReferences() internally).

Parameters:
Return type:

Optional[QgsLayerTreeNode]

removeChildrenPrivate(self, from_: int, count: int, destroy: bool = True)[source]

Low-level removal of children from the node.

Parameters:
  • from (int)

  • count (int)

  • destroy (bool = True)

removeCustomProperty(self, key: str | None)[source]

Remove a custom property from layer. Properties are stored in a map and saved in project file.

Parameters:

key (Optional[str])

signal removedChildren[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.

abstract resolveReferences(self, project: QgsProject | None, looseMatching: bool = False)[source]

Turn textual references to layers into map layer object from project. This method should be called after readXml() If looseMatching is True then a looser match will be used, where a layer will match if the name, public source, and data provider match. This can be used to match legend customization from different projects where layers will have different layer IDs.

Parameters:
  • project (Optional[QgsProject])

  • looseMatching (bool = False)

setCustomProperty(self, key: str | None, value: Any)[source]

Sets a custom property for the node. Properties are stored in a map and saved in project file.

Parameters:
  • key (Optional[str])

  • value (Any)

setExpanded(self, expanded: bool)[source]

Sets whether the node should be shown as expanded or collapsed in GUI

Parameters:

expanded (bool)

setItemVisibilityChecked(self, checked: bool)[source]

Check or uncheck a node (independently of its ancestors or children)

See also

QgsLayerTreeView.setLayerVisible()

Parameters:

checked (bool)

setItemVisibilityCheckedParentRecursive(self, checked: bool)[source]

Check or uncheck a node and all its parents

Parameters:

checked (bool)

virtual setItemVisibilityCheckedRecursive(self, checked: bool)[source]

Check or uncheck a node and all its children (taking into account exclusion rules)

Parameters:

checked (bool)

abstract setName(self, name: str | None)[source]

Set name of the node. Emits nameChanged signal.

Parameters:

name (Optional[str])

takeChild(self, node: QgsLayerTreeNode | None) bool[source]

Remove a child from a node

Parameters:

node (Optional[QgsLayerTreeNode])

Return type:

bool

signal visibilityChanged[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.

signal willAddChildren[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.

signal willRemoveChildren[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.

writeCommonXml(self, element: QDomElement)[source]

Write common XML elements.

Parameters:

element (QDomElement)

abstract writeXml(self, parentElement: QDomElement, context: QgsReadWriteContext)[source]

Write layer tree to XML

Parameters: