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.
See also

QgsLayerTreeView¶
Class Hierarchy¶
Base classes¶
Methods
Adds an indicator to the given layer tree node. |
|
Enhancement of QTreeView.collapseAll() that also records expanded state in layer tree nodes |
|
Gets current group node. |
|
Returns the currently selected layer, or |
|
Gets current legend node. |
|
Gets current node. |
|
Gets access to the default actions that may be used with the tree view |
|
Enhancement of QTreeView.expandAll() that also records expanded state in layer tree nodes |
|
Returns if valid layers should be hidden (i.e. only invalid layers are shown). |
|
Returns legend node for given proxy model tree index. |
|
Returns layer tree node for given proxy model tree index. |
|
Returns list of indicators associated with a particular layer tree node. |
|
Returns width of contextual menu mark, at right of layer node items. |
|
Gets access to the model casted to |
|
Returns proxy model index for a given legend node. |
|
Returns index for a given legend node. |
|
Returns pointer to the context menu provider. |
|
Returns proxy model index for a given node. |
|
Returns source model index for a given node. |
|
Returns the proxy model used by the view. |
|
Force refresh of layer symbology. |
|
Removes a previously added indicator to a layer tree node. |
|
Returns the list of selected nodes filtered to just layer nodes ( |
|
Returns the list of selected layers. |
|
Gets list of selected layers, including those that are not directly selected, but their ancestor groups is selected. |
|
Returns the list of selected legend nodes. |
|
Returns the list of selected layer tree nodes. |
|
Sets the currently selected layer. |
|
Sets the currently selected node. |
|
Sets whether valid layers should be hidden (i.e. only invalid layers are shown). |
|
Set width of contextual menu mark, at right of layer node items. |
|
Convenience methods which sets the visible state of the specified map layer. |
|
Sets provider for context menu. |
|
Set the message bar to display messages from the layer tree |
|
Set the show private layers to showPrivate |
|
Returns the show private layers status |
|
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.
Overridden |
Signals
Emitted when the context menu is about to show. |
|
Emitted when a current layer is changed |
|
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
See also
Added in version 3.2.
- Parameters:
node (Optional[QgsLayerTreeNode])
indicator (Optional[QgsLayerTreeViewIndicator])
- 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.See also
- 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).
See also
Added in version 3.38.
- Return type:
bool
- index2legendNode(self, index: QModelIndex) QgsLayerTreeModelLegendNode | None [source]¶
Returns legend node for given proxy model tree
index
. ReturnsNone
for invalid indexUnlike
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. ReturnsNone
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
See also
Added in version 3.2.
- Parameters:
node (Optional[QgsLayerTreeNode])
- Return type:
- 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.
See also
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
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)
- 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
- onExpandedChanged(self, node: QgsLayerTreeNode | None, expanded: bool)[source]¶
- Parameters:
node (Optional[QgsLayerTreeNode])
expanded (bool)
- 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
See also
Added in version 3.2.
- Parameters:
node (Optional[QgsLayerTreeNode])
indicator (Optional[QgsLayerTreeViewIndicator])
- selectedLayerNodes(self) List[QgsLayerTreeLayer] ¶
Returns the list of selected nodes filtered to just layer nodes (
QgsLayerTreeLayer
).See also
See also
See also
- Return type:
List[QgsLayerTreeLayer]
- selectedLayers(self) List[QgsMapLayer] ¶
Returns the list of selected layers.
See also
See also
See also
- 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
See also
Added in version 3.32.
- Return type:
- 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
See also
See also
- Return type:
List[QgsLayerTreeNode]
- setCurrentLayer(self, layer: QgsMapLayer | None)[source]¶
Sets the currently selected
layer
.If
layer
isNone
then all layers will be deselected.See also
- Parameters:
layer (Optional[QgsMapLayer])
- setCurrentNode(self, node: QgsLayerTreeNode | None)[source]¶
Sets the currently selected
node
.If
node
isNone
then all nodes will be deselected.See also
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).
See also
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
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:
layer (Optional[QgsMapLayer])
visible (bool)
- 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. OnlyQgsLayerTreeModel
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
andproxyModel
for the view.Use this method when a custom proxy model is required.
Added in version 4.0.
- Parameters:
model (Optional[QgsLayerTreeModel])
proxyModel (Optional[QgsLayerTreeProxyModel])
- 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])