Class: QgsLayerTreeViewBase

Base class for QTreeView widgets which display a layer tree.

The view updates expanded state of layer tree nodes and also listens to changes to expanded states in the layer tree.

Warning

Subclasses must take care to call both setLayerTreeModel() and QTreeView.setModel() in order to have a fully functional tree view. This is by design, as it permits use of a custom proxy model in the view.

See also

QgsLayerTreeView

Added in version 4.0.

Class Hierarchy

Inheritance diagram of qgis.gui.QgsLayerTreeViewBase

Base classes

QTreeView

QAbstractItemView

QAbstractScrollArea

QFrame

QWidget

QObject

QPaintDevice

Subclasses

QgsLayerTreeView

Extends QTreeView and provides additional functionality when working with a layer tree.

Methods

collapseAllNodes

Enhancement of QTreeView.collapseAll() that also records expanded state in layer tree nodes

currentGroupNode

Returns the current group node.

currentLayer

Returns the currently selected layer, or None if no layers is selected.

currentLegendNode

Gets current legend node.

currentNode

Returns the current node.

defaultActions

Gets access to the default actions that may be used with the tree view

expandAllNodes

Enhancement of QTreeView.expandAll() that also records expanded state in layer tree nodes

index2legendNode

Returns legend node for given view index.

index2node

Returns the layer tree node for given view index.

layerForIndex

Returns the map layer corresponding to a view index.

layerTreeModel

Returns the associated layer tree model.

layerTreeModelIndexToViewIndex

Returns the layer tree model index corresponding with a view index.

legendNode2index

Returns the view index for a given legend node.

legendNode2sourceIndex

Returns the layer tree source model index for a given legend node.

node2index

Returns the view model index for a given node.

node2sourceIndex

Returns the layer tree source model index for a given node.

onExpandedChanged

Called when the expanded state changes for a node.

onModelReset

Called when the model is reset.

selectedLayerNodes

Returns the list of selected nodes filtered to just layer nodes (QgsLayerTreeLayer).

selectedLayers

Returns the list of selected layers.

selectedLayersRecursive

Gets list of selected layers, including those that are not directly selected, but their ancestor groups is selected.

selectedLegendNodes

Returns the list of selected legend nodes.

selectedNodes

Returns the list of selected layer tree nodes.

setCurrentLayer

Sets the currently selected layer.

setCurrentNode

Sets the currently selected node.

setLayerTreeModel

Associates a layer tree model with the view.

updateExpandedStateFromNode

Updates the expanded state from a node.

updateExpandedStateToNode

Stores the expanded state to a node with matching index.

viewIndexToLayerTreeModelIndex

Returns the view index corresponding with a layer tree model index.

class qgis.gui.QgsLayerTreeViewBase[source]

Bases: QTreeView

__init__(parent: QWidget | None = None)

Constructor for QgsLayerTreeViewBase

Parameters:

parent (Optional[QWidget] = None)

collapseAllNodes(self)[source]

Enhancement of QTreeView.collapseAll() that also records expanded state in layer tree nodes

currentGroupNode(self) QgsLayerTreeGroup | None[source]

Returns the current group node.

If a layer is the current node, the function will return the layer’s parent group.

May be None.

Return type:

Optional[QgsLayerTreeGroup]

currentLayer(self) QgsMapLayer | None[source]

Returns the currently selected layer, or None if no layers is selected.

Return type:

Optional[QgsMapLayer]

currentLegendNode(self) QgsLayerTreeModelLegendNode | None[source]

Gets current legend node. May be None if current node is not a legend node.

Return type:

Optional[QgsLayerTreeModelLegendNode]

currentNode(self) QgsLayerTreeNode | None[source]

Returns the current node.

May be None.

Return type:

Optional[QgsLayerTreeNode]

defaultActions(self) QgsLayerTreeViewDefaultActions | None[source]

Gets access to the default actions that may be used with the tree view

Return type:

Optional[QgsLayerTreeViewDefaultActions]

expandAllNodes(self)[source]

Enhancement of QTreeView.expandAll() that also records expanded state in layer tree nodes

index2legendNode(self, index: QModelIndex) QgsLayerTreeModelLegendNode | None[source]

Returns legend node for given view index.

Returns None for invalid index.

Unlike QgsLayerTreeModel.index2legendNode(), calling this method correctly accounts for mapping the view indexes through the view’s proxy model to the source model.

Added in version 3.18.

Parameters:

index (QModelIndex)

Return type:

Optional[QgsLayerTreeModelLegendNode]

index2node(self, index: QModelIndex) QgsLayerTreeNode | None[source]

Returns the layer tree node for given view index.

Returns root node for an invalid index.

Returns None if index does not refer to a layer tree node (e.g. it is a legend node).

Unlike QgsLayerTreeViewBase.index2Node(), calling this method correctly accounts for mapping the view indexes through the view’s proxy model to the source model.

See also

node2index()

Added in version 3.18.

Parameters:

index (QModelIndex)

Return type:

Optional[QgsLayerTreeNode]

layerForIndex(self, index: QModelIndex) QgsMapLayer | None[source]

Returns the map layer corresponding to a view index.

This method correctly accounts for proxy models set on the tree view.

Returns None if the index does not correspond to a map layer.

Parameters:

index (QModelIndex)

Return type:

Optional[QgsMapLayer]

layerTreeModel(self) QgsLayerTreeModel | None[source]

Returns the associated layer tree model.

Return type:

Optional[QgsLayerTreeModel]

layerTreeModelIndexToViewIndex(self, index: QModelIndex) QModelIndex[source]

Returns the layer tree model index corresponding with a view index.

This method correctly accounts for proxy models set on the tree view.

Parameters:

index (QModelIndex)

Return type:

QModelIndex

legendNode2index(self, legendNode: QgsLayerTreeModelLegendNode | None) QModelIndex[source]

Returns the view index for a given legend node.

If the legend node does not belong to the layer tree, the result is undefined.

If the legend node belongs to the tree but it is filtered out, an invalid model index is returned.

Unlike QgsLayerTreeModel.legendNode2index(), calling this method correctly accounts for mapping the view indexes through the view’s proxy model to the source model.

Added in version 3.18.

Parameters:

legendNode (Optional[QgsLayerTreeModelLegendNode])

Return type:

QModelIndex

legendNode2sourceIndex(self, legendNode: QgsLayerTreeModelLegendNode | None) QModelIndex[source]

Returns the layer tree source model index for a given legend node.

If the legend node does not belong to the layer tree, the result is undefined.

If the legend node belongs to the tree but it is filtered out, an invalid model index is returned.

Warning

The returned index belongs the underlying layer tree model, and care should be taken to correctly map to a proxy index if a proxy model is in use.

Added in version 3.18.

Parameters:

legendNode (Optional[QgsLayerTreeModelLegendNode])

Return type:

QModelIndex

node2index(self, node: QgsLayerTreeNode | None) QModelIndex[source]

Returns the view model index for a given node.

If the node does not belong to the layer tree, the result is undefined.

Unlike QgsLayerTreeModel.node2index(), calling this method correctly accounts for mapping the view indexes through the view’s proxy model to the source model.

See also

index2node()

Added in version 3.18.

Parameters:

node (Optional[QgsLayerTreeNode])

Return type:

QModelIndex

node2sourceIndex(self, node: QgsLayerTreeNode | None) QModelIndex[source]

Returns the layer tree source model index for a given node.

If the node does not belong to the layer tree, the result is undefined.

Warning

The returned index belongs the underlying layer tree model, and care should be taken to correctly map to a proxy index if a proxy model is in use.

See also

node2index()

Added in version 3.18.

Parameters:

node (Optional[QgsLayerTreeNode])

Return type:

QModelIndex

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

Called when the expanded state changes for a node.

Parameters:
onModelReset(self)[source]

Called when the model is reset.

selectedLayerNodes(self) List[QgsLayerTreeLayer]

Returns the list of selected nodes filtered to just layer nodes (QgsLayerTreeLayer).

See also

selectedNodes()

See also

selectedLayers()

Return type:

List[QgsLayerTreeLayer]

selectedLayers(self) List[QgsMapLayer]

Returns the list of selected layers.

See also

selectedNodes()

Return type:

List[QgsMapLayer]

selectedLayersRecursive(self) List[QgsMapLayer]

Gets list of selected layers, including those that are not directly selected, but their ancestor groups is selected. If we have a group with two layers L1, L2 and just the group node is selected, this method returns L1 and L2, while selectedLayers() returns an empty list.

Added in version 3.4.

Return type:

List[QgsMapLayer]

selectedLegendNodes(self) List[QgsLayerTreeModelLegendNode]

Returns the list of selected legend nodes.

See also

selectedNodes()

Added in version 3.32.

Return type:

List[QgsLayerTreeModelLegendNode]

selectedNodes(self, skipInternal: bool = False) List[QgsLayerTreeNode]

Returns the list of selected layer tree nodes.

Parameters:

skipInternal (bool = False) – If True, will ignore nodes which have an ancestor in the selection

See also

selectedLayers()

Return type:

List[QgsLayerTreeNode]

setCurrentLayer(self, layer: QgsMapLayer | None)[source]

Sets the currently selected layer.

If layer is None then all layers will be deselected.

See also

currentLayer()

Parameters:

layer (Optional[QgsMapLayer])

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

Sets the currently selected node.

If node is None then all nodes will be deselected.

See also

currentNode()

Added in version 3.40.

Parameters:

node (Optional[QgsLayerTreeNode])

setLayerTreeModel(self, model: QgsLayerTreeModel | None)[source]

Associates a layer tree model with the view.

Warning

This does NOT explicitly set the view’s model, and a subsequent call to QTreeView.setModel() must be made. This is by design, as it permits use of a custom proxy model in the view.

See also

layerTreeModel()

Parameters:

model (Optional[QgsLayerTreeModel])

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

Updates the expanded state from a node.

Parameters:

node (Optional[QgsLayerTreeNode])

updateExpandedStateToNode(self, index: QModelIndex)[source]

Stores the expanded state to a node with matching index.

Parameters:

index (QModelIndex)

viewIndexToLayerTreeModelIndex(self, index: QModelIndex) QModelIndex[source]

Returns the view index corresponding with a layer tree model index.

This method correctly accounts for proxy models set on the tree view.

Parameters:

index (QModelIndex)

Return type:

QModelIndex