Class: QgsLayerTreeView

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

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

The view keeps track of the current layer and emits a signal when the current layer has changed.

Allows the client to specify a context menu provider with custom actions. Also it comes with a set of default actions that can be used when building context menu.

QgsLayerTreeView

QgsLayerTreeView

Class Hierarchy

Inheritance diagram of qgis.gui.QgsLayerTreeView

Base classes

QTreeView

QAbstractItemView

QAbstractScrollArea

QFrame

QWidget

QObject

QPaintDevice

Methods

addIndicator

Adds an indicator to the given layer tree node.

collapseAllNodes

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

currentGroupNode

Gets current group node.

currentLayer

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

currentLegendNode

Gets current legend node.

currentNode

Gets 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

hideValidLayers

Returns if valid layers should be hidden (i.e. only invalid layers are shown).

index2legendNode

Returns legend node for given proxy model tree index.

index2node

Returns layer tree node for given proxy model tree index.

indicators

Returns list of indicators associated with a particular layer tree node.

layerForIndex

layerMarkWidth

Returns width of contextual menu mark, at right of layer node items.

layerTreeModel

Gets access to the model casted to QgsLayerTreeModel

legendNode2index

Returns proxy model index for a given legend node.

legendNode2sourceIndex

Returns index for a given legend node.

menuProvider

Returns pointer to the context menu provider.

modelRowsInserted

modelRowsRemoved

node2index

Returns proxy model index for a given node.

node2sourceIndex

Returns source model index for a given node.

onCurrentChanged

onExpandedChanged

onModelReset

proxyModel

Returns the proxy model used by the view.

refreshLayerSymbology

Force refresh of layer symbology.

removeIndicator

Removes a previously added indicator to a layer tree node.

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.

setHideValidLayers

Sets whether valid layers should be hidden (i.e. only invalid layers are shown).

setLayerMarkWidth

Set width of contextual menu mark, at right of layer node items.

setLayerVisible

Convenience methods which sets the visible state of the specified map layer.

setMenuProvider

Sets provider for context menu.

setMessageBar

Set the message bar to display messages from the layer tree

setShowPrivateLayers

Set the show private layers to showPrivate

showPrivateLayers

Returns the show private layers status

updateExpandedStateFromNode

updateExpandedStateToNode

Virtual Methods

In PyQGIS, only methods marked as virtual can be safely overridden in a Python subclass of QgsLayerTreeView. See the FAQ for more details.

setModel

Overridden setModel() from base class.

Signals

contextMenuAboutToShow

Emitted when the context menu is about to show.

currentLayerChanged

Emitted when a current layer is changed

datasetsDropped

Emitted when datasets are dropped onto the layer tree view

class qgis.gui.QgsLayerTreeView[source]

Bases: QTreeView

__init__(parent: QWidget | None = None)

Constructor for QgsLayerTreeView

Parameters:

parent (Optional[QWidget] = None)

addIndicator(self, node: QgsLayerTreeNode | None, indicator: QgsLayerTreeViewIndicator | None)[source]

Adds an indicator to the given layer tree node. Indicators are icons shown next to layer/group names in the layer tree view. They can be used to show extra information with tree nodes and they allow user interaction.

Does not take ownership of the indicator. One indicator object may be used for multiple layer tree nodes.

See also

indicators()

Added in version 3.2.

Parameters:
collapseAllNodes(self)[source]

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

signal contextMenuAboutToShow(menu: QMenu)[source]

Emitted when the context menu is about to show.

Allows customization of the menu.

Added in version 3.32.

Parameters:

menu (QMenu)

currentGroupNode(self) QgsLayerTreeGroup | None[source]

Gets current group node. If a layer is current node, the function will return 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]

signal currentLayerChanged(layer: QgsMapLayer)[source]

Emitted when a current layer is changed

Parameters:

layer (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]

Gets current node. May be None

Return type:

Optional[QgsLayerTreeNode]

signal datasetsDropped(event: QDropEvent)[source]

Emitted when datasets are dropped onto the layer tree view

Parameters:

event (QDropEvent)

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

hideValidLayers(self) bool[source]

Returns if valid layers should be hidden (i.e. only invalid layers are shown).

Added in version 3.38.

Return type:

bool

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

Returns legend node for given proxy model tree 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 layer tree node for given proxy model tree index. Returns root node for invalid index. Returns None if index does not refer to a layer tree node (e.g. it is a legend node)

Unlike QgsLayerTreeModel.index2Node(), 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[QgsLayerTreeNode]

indicators(self, node: QgsLayerTreeNode | None) List[QgsLayerTreeViewIndicator]

Returns list of indicators associated with a particular layer tree node.

See also

addIndicator()

Added in version 3.2.

Parameters:

node (Optional[QgsLayerTreeNode])

Return type:

List[QgsLayerTreeViewIndicator]

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

index (QModelIndex)

Return type:

Optional[QgsMapLayer]

layerMarkWidth(self) int[source]

Returns width of contextual menu mark, at right of layer node items.

Added in version 3.8.

Return type:

int

layerTreeModel(self) QgsLayerTreeModel | None[source]

Gets access to the model casted to QgsLayerTreeModel

Return type:

Optional[QgsLayerTreeModel]

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

Returns proxy 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 is belongs to the tree but it is filtered out, 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 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 is belongs to the tree but it is filtered out, invalid model index is returned.

Added in version 3.18.

Parameters:

legendNode (Optional[QgsLayerTreeModelLegendNode])

Return type:

QModelIndex

menuProvider(self) QgsLayerTreeViewMenuProvider | None[source]

Returns pointer to the context menu provider. May be None

Return type:

Optional[QgsLayerTreeViewMenuProvider]

modelRowsInserted(self, index: QModelIndex, start: int, end: int)[source]
Parameters:
  • index (QModelIndex)

  • start (int)

  • end (int)

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

Returns proxy 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.

Added in version 3.18.

Parameters:

node (Optional[QgsLayerTreeNode])

Return type:

QModelIndex

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

Returns source model index for a given node. If the node does not belong to the layer tree, the result is undefined

Added in version 3.18.

Parameters:

node (Optional[QgsLayerTreeNode])

Return type:

QModelIndex

onCurrentChanged(self)[source]
onExpandedChanged(self, node: QgsLayerTreeNode | None, expanded: bool)[source]
Parameters:
onModelReset(self)[source]
proxyModel(self) QgsLayerTreeProxyModel | None[source]

Returns the proxy model used by the view.

This can be used to set filters controlling which layers are shown in the view.

Added in version 3.18.

Return type:

Optional[QgsLayerTreeProxyModel]

refreshLayerSymbology(self, layerId: str | None)[source]

Force refresh of layer symbology. Normally not needed as the changes of layer’s renderer are monitored by the model

Parameters:

layerId (Optional[str])

removeIndicator(self, node: QgsLayerTreeNode | None, indicator: QgsLayerTreeViewIndicator | None)[source]

Removes a previously added indicator to a layer tree node. Does not delete the indicator.

See also

addIndicator()

See also

indicators()

Added in version 3.2.

Parameters:
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])

setHideValidLayers(self, hideValid: bool)[source]

Sets whether valid layers should be hidden (i.e. only invalid layers are shown).

Added in version 3.38.

Parameters:

hideValid (bool)

setLayerMarkWidth(self, width: int)[source]

Set width of contextual menu mark, at right of layer node items.

See also

layerMarkWidth()

Added in version 3.8.

Parameters:

width (int)

setLayerVisible(self, layer: QgsMapLayer | None, visible: bool)[source]

Convenience methods which sets the visible state of the specified map layer.

See also

QgsLayerTreeNode.setItemVisibilityChecked()

Added in version 3.10.

Parameters:
setMenuProvider(self, menuProvider: QgsLayerTreeViewMenuProvider | None)[source]

Sets provider for context menu. Takes ownership of the instance

Parameters:

menuProvider (Optional[QgsLayerTreeViewMenuProvider])

setMessageBar(self, messageBar: QgsMessageBar | None)[source]

Set the message bar to display messages from the layer tree

Added in version 3.14.

Parameters:

messageBar (Optional[QgsMessageBar])

virtual setModel(self, model: QAbstractItemModel | None)[source]

Overridden setModel() from base class. Only QgsLayerTreeModel is an acceptable model.

Note

This method automatically creates a QgsLayerTreeProxyModel to use as a proxy.

Parameters:

model (Optional[QAbstractItemModel])

virtual setModel(self, model: QgsLayerTreeModel | None, proxyModel: QgsLayerTreeProxyModel | None)[source]

Sets the model and proxyModel for the view.

Use this method when a custom proxy model is required.

Added in version 4.0.

Parameters:
setShowPrivateLayers(self, showPrivate: bool)[source]

Set the show private layers to showPrivate

Added in version 3.18.

Parameters:

showPrivate (bool)

showPrivateLayers(self) bool[source]

Returns the show private layers status

Added in version 3.18.

Return type:

bool

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

node (Optional[QgsLayerTreeNode])

updateExpandedStateToNode(self, index: QModelIndex)[source]
Parameters:

index (QModelIndex)