Class: QgsLayerTreeUtils

class qgis.core.QgsLayerTreeUtils

Bases: sip.wrapper

Assorted functions for dealing with layer trees.

New in version 2.4: Enums

Methods

checkStateFromXml

Convert QString to Qt.CheckState

checkStateToXml

Convert Qt.CheckState to QString

collectMapLayersRecursive

Returns map layers from the given list of layer tree nodes.

countMapLayerInTree

Returns how many occurrences of a map layer are there in a layer tree.

firstGroupWithoutCustomProperty

Returns the first parent which doesn’t have the given custom property or the group itself if it doesn’t hold the property

hasLegendFilterExpression

Test if one of the layers in a group has an expression filter

insertLayerBelow

Insert a QgsMapLayer just below another one

invisibleLayerList

Gets invisible layers

layersEditable

Returns True if any of the layers is editable

layersModified

Returns True if any of the layers is modified

legendFilterByExpression

Returns the expression filter of a legend layer

readOldLegend

Try to load layer tree from verbatim <legend> endverbatim tag from project files from QGIS 2.2 and below

readOldLegendLayerOrder

Try to load custom layer order from verbatim <legend> endverbatim tag from project files from QGIS 2.2 and below

removeInvalidLayers

Removes layer nodes that refer to invalid layers

replaceChildrenOfEmbeddedGroups

Remove subtree of embedded groups and replaces it with a custom property embedded-visible-layers

setLegendFilterByExpression

Sets the expression filter of a legend layer

storeOriginalLayersProperties

Stores in a layer’s originalXmlProperties the layer properties information

updateEmbeddedGroupsProjectPath

Updates an embedded group from a project.

writeOldLegend

Returns verbatim <legend> endverbatim tag used in QGIS 2.2 and below

Signals

Attributes

checkStateFromXml(txt: str) → Qt.CheckState

Convert QString to Qt.CheckState

checkStateToXml(state: Qt.CheckState) → str

Convert Qt.CheckState to QString

collectMapLayersRecursive(nodes: Iterable[QgsLayerTreeNode]) → Set[QgsMapLayer]

Returns map layers from the given list of layer tree nodes. Also recursively visits child nodes of groups.

New in version 3.4.

countMapLayerInTree(tree: QgsLayerTreeNode, layer: QgsMapLayer) → int

Returns how many occurrences of a map layer are there in a layer tree. In normal situations there is at most one occurrence, but sometimes there may be temporarily more: for example, during drag&drop, upon drop a new layer node is created while the original dragged node is still in the tree, resulting in two occurrences.

This is useful when deciding whether to start or stop listening to a signal of a map layer within a layer tree and only connecting/disconnecting when there is only one occurrence of that layer.

New in version 3.4.

firstGroupWithoutCustomProperty(group: QgsLayerTreeGroup, property: str) → QgsLayerTreeGroup

Returns the first parent which doesn’t have the given custom property or the group itself if it doesn’t hold the property

Parameters
  • group – the layer tree group

  • property – the property

New in version 3.8.

hasLegendFilterExpression(group: QgsLayerTreeGroup) → bool

Test if one of the layers in a group has an expression filter

insertLayerBelow(group: QgsLayerTreeGroup, refLayer: QgsMapLayer, layerToInsert: QgsMapLayer) → QgsLayerTreeLayer

Insert a QgsMapLayer just below another one

Parameters
  • group – the tree group where layers are (can be the root group)

  • refLayer – the reference layer

  • layerToInsert – the new layer to insert just below the reference layer

Returns

the new tree layer

invisibleLayerList(node: QgsLayerTreeNode) → List[str]

Gets invisible layers

layersEditable(layerNodes: Iterable[QgsLayerTreeLayer]) → bool

Returns True if any of the layers is editable

layersModified(layerNodes: Iterable[QgsLayerTreeLayer]) → bool

Returns True if any of the layers is modified

legendFilterByExpression(layer: QgsLayerTreeLayer) → Tuple[str, bool]

Returns the expression filter of a legend layer

readOldLegend(root: QgsLayerTreeGroup, legendElem: QDomElement) → bool

Try to load layer tree from verbatim <legend> endverbatim tag from project files from QGIS 2.2 and below

readOldLegendLayerOrder(legendElem: QDomElement, order: Iterable[str]) → Tuple[bool, bool]

Try to load custom layer order from verbatim <legend> endverbatim tag from project files from QGIS 2.2 and below

removeInvalidLayers(group: QgsLayerTreeGroup)

Removes layer nodes that refer to invalid layers

replaceChildrenOfEmbeddedGroups(group: QgsLayerTreeGroup)

Remove subtree of embedded groups and replaces it with a custom property embedded-visible-layers

setLegendFilterByExpression(layer: QgsLayerTreeLayer, expr: str, enabled: bool = True)

Sets the expression filter of a legend layer

storeOriginalLayersProperties(group: QgsLayerTreeGroup, doc: QDomDocument)

Stores in a layer’s originalXmlProperties the layer properties information

New in version 3.6.

updateEmbeddedGroupsProjectPath(group: QgsLayerTreeGroup, project: QgsProject)

Updates an embedded group from a project.

writeOldLegend(doc: QDomDocument, root: QgsLayerTreeGroup, hasCustomOrder: bool, order: Iterable[QgsMapLayer]) → QDomElement

Returns verbatim <legend> endverbatim tag used in QGIS 2.2 and below