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
See also
See also
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¶
Base classes¶
Subclasses¶
Layer tree group node serves as a container for layers and further groups. |
|
Layer tree node points to a map layer. |
Abstract Methods
Create a copy of the node. |
|
Returns string with layer tree structure. |
|
Returns name of the node |
|
Turn textual references to layers into map layer object from project. |
|
Set name of the node. |
|
Write layer tree to XML |
Methods
Returns a list of any checked layers which belong to this node or its children. |
|
Gets list of children of the node. |
|
Returns list of keys stored in custom properties |
|
Read a custom property from layer. |
|
Returns the depth of this node, i.e. the number of its ancestors. |
|
Low-level insertion of children to the node. |
|
Returns whether the node should be shown as expanded or collapsed in GUI |
|
Returns whether this node is checked and all its children. |
|
Returns whether this node is unchecked and all its children. |
|
Returns whether a node is really visible (ie checked and all its ancestors checked as well) |
|
Returns whether a node is checked (independently of its ancestors or children) |
|
Find out about type of the node. |
|
Gets pointer to the parent. |
|
Read common XML elements. |
|
Low-level removal of children from the node. |
|
Remove a custom property from layer. |
|
Sets a custom property for the node. |
|
Sets whether the node should be shown as expanded or collapsed in GUI |
|
Check or uncheck a node (independently of its ancestors or children) |
|
Check or uncheck a node and all its parents |
|
Remove a child from a node |
|
Write common XML elements. |
Virtual Methods
In PyQGIS, only methods marked as virtual
can be safely overridden in a Python subclass of QgsLayerTreeNode. See the FAQ for more details.
Check or uncheck a node and all its children (taking into account exclusion rules) |
Static Methods
Read layer tree from XML. |
Signals
Emitted when one or more nodes have been added to a node within the tree |
|
Emitted when a custom property of a node within the tree has been changed or removed |
|
Emitted when the collapsed/expanded state of a node within the tree has been changed |
|
Emitted when the name of the node is changed |
|
Emitted when one or more nodes has been removed from a node within the tree |
|
Emitted when check state of a node within the tree has been changed |
|
Emitted when one or more nodes will be added to a node within the tree |
|
Emitted when one or more nodes will be removed from a node within the tree |
Attributes
- class qgis.core.QgsLayerTreeNode[source]¶
Bases:
QObject
- __init__(t: QgsLayerTreeNode.NodeType, checked: bool = True)
Constructor
- Parameters:
checked (bool = True)
- __init__(other: QgsLayerTreeNode)
- Parameters:
other (QgsLayerTreeNode)
- NodeGroup = 0¶
- NodeLayer = 1¶
- class NodeType¶
Bases:
int
- signal addedChildren(node: QgsLayerTreeNode, indexFrom: int, indexTo: int)[source]¶
Emitted when one or more nodes have been added to a node within the tree
- Parameters:
node (QgsLayerTreeNode)
indexFrom (int)
indexTo (int)
- 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(node: QgsLayerTreeNode, key: str)[source]¶
Emitted when a custom property of a node within the tree has been changed or removed
- Parameters:
node (QgsLayerTreeNode)
key (str)
- 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(node: QgsLayerTreeNode, expanded: bool)[source]¶
Emitted when the collapsed/expanded state of a node within the tree has been changed
- Parameters:
node (QgsLayerTreeNode)
expanded (bool)
- 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:
index (int)
nodes (Iterable[QgsLayerTreeNode])
- 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
- signal nameChanged(node: QgsLayerTreeNode, name: str)[source]¶
Emitted when the name of the node is changed
- Parameters:
node (QgsLayerTreeNode)
name (str)
- 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:
- 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.- Parameters:
element (QDomElement)
context (
QgsReadWriteContext
)
- Return type:
Optional[QgsLayerTreeNode]
- static readXml(element: QDomElement, project: QgsProject | None) QgsLayerTreeNode | None [source]
Read layer tree from XML. Returns new instance. Also resolves textual references to layers from the project (calls
resolveReferences()
internally).- Parameters:
element (QDomElement)
project (Optional[QgsProject])
- 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(node: QgsLayerTreeNode, indexFrom: int, indexTo: int)[source]¶
Emitted when one or more nodes has been removed from a node within the tree
- Parameters:
node (QgsLayerTreeNode)
indexFrom (int)
indexTo (int)
- 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()
IflooseMatching
isTrue
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(node: QgsLayerTreeNode)[source]¶
Emitted when check state of a node within the tree has been changed
- Parameters:
node (QgsLayerTreeNode)
- signal willAddChildren(node: QgsLayerTreeNode, indexFrom: int, indexTo: int)[source]¶
Emitted when one or more nodes will be added to a node within the tree
- Parameters:
node (QgsLayerTreeNode)
indexFrom (int)
indexTo (int)
- signal willRemoveChildren(node: QgsLayerTreeNode, indexFrom: int, indexTo: int)[source]¶
Emitted when one or more nodes will be removed from a node within the tree
- Parameters:
node (QgsLayerTreeNode)
indexFrom (int)
indexTo (int)
- 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:
parentElement (QDomElement)
context (QgsReadWriteContext)