Class: QgsLayerTreeUtils

class qgis.core.QgsLayerTreeUtils

Bases: sip.wrapper

Assorted functions for dealing with layer trees.

New in version 2.4:

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 specified 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

checkStateFromXml(txt: str) Qt.CheckState

Convert QString to Qt.CheckState

Parameters:

txt (str) –

Return type:

Qt.CheckState

checkStateToXml(state: Qt.CheckState) str

Convert Qt.CheckState to QString

Parameters:

state (Qt.CheckState) –

Return type:

str

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.

Parameters:

nodes (Iterable[QgsLayerTreeNode]) –

Return type:

Set[QgsMapLayer]

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.

Parameters:
Return type:

int

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:

New in version 3.8.

Return type:

QgsLayerTreeGroup

hasLegendFilterExpression(group: QgsLayerTreeGroup) bool

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

Parameters:

group (QgsLayerTreeGroup) –

Return type:

bool

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

Insert a QgsMapLayer just below another one

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

  • refLayer (QgsMapLayer) – the reference layer

  • layerToInsert (QgsMapLayer) – the new layer to insert just below the reference layer

Return type:

QgsLayerTreeLayer

Returns:

the new tree layer

invisibleLayerList(node: QgsLayerTreeNode) List[str]

Gets invisible layers

Parameters:

node (QgsLayerTreeNode) –

Return type:

List[str]

layersEditable(layerNodes: Iterable[QgsLayerTreeLayer], ignoreLayersWhichCannotBeToggled: bool = False) bool

Returns True if any of the specified layers is editable.

The ignoreLayersWhichCannotBeToggled argument can be used to control whether layers which cannot have their edit states toggled by users should be ignored or not (since QGIS 3.22).

Parameters:
  • layerNodes (Iterable[QgsLayerTreeLayer]) –

  • ignoreLayersWhichCannotBeToggled (bool = False) –

Return type:

bool

layersModified(layerNodes: Iterable[QgsLayerTreeLayer]) bool

Returns True if any of the layers is modified

Parameters:

layerNodes (Iterable[QgsLayerTreeLayer]) –

Return type:

bool

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

Returns the expression filter of a legend layer

Parameters:

layer (QgsLayerTreeLayer) –

Return type:

Tuple[str, bool]

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

Parameters:
Return type:

bool

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

Parameters:
  • legendElem (QDomElement) –

  • order (Iterable[str]) –

Return type:

Tuple[bool, bool]

removeInvalidLayers(group: QgsLayerTreeGroup)

Removes layer nodes that refer to invalid layers

Parameters:

group (QgsLayerTreeGroup) –

replaceChildrenOfEmbeddedGroups(group: QgsLayerTreeGroup)

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

Parameters:

group (QgsLayerTreeGroup) –

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

Sets the expression filter of a legend layer

Parameters:
storeOriginalLayersProperties(group: QgsLayerTreeGroup, doc: QDomDocument)

Stores in a layer’s originalXmlProperties the layer properties information

New in version 3.6.

Parameters:
updateEmbeddedGroupsProjectPath(group: QgsLayerTreeGroup, project: QgsProject)

Updates an embedded group from a project.

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

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

Parameters:
Return type:

QDomElement