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
Added in version 4.0.
Class Hierarchy¶
Base classes¶
Subclasses¶
Extends QTreeView and provides additional functionality when working with a layer tree. |
Methods
Enhancement of QTreeView.collapseAll() that also records expanded state in layer tree nodes |
|
Returns the current group node. |
|
Returns the currently selected layer, or |
|
Gets current legend node. |
|
Returns the 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 legend node for given view index. |
|
Returns the layer tree node for given view index. |
|
Returns the map layer corresponding to a view index. |
|
Returns the associated layer tree model. |
|
Returns the layer tree model index corresponding with a view index. |
|
Returns the view index for a given legend node. |
|
Returns the layer tree source model index for a given legend node. |
|
Returns the view model index for a given node. |
|
Returns the layer tree source model index for a given node. |
|
Called when the expanded state changes for a node. |
|
Called when the model is reset. |
|
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. |
|
Associates a layer tree model with the view. |
|
Updates the expanded state from a node. |
|
Stores the expanded state to a node with matching index. |
|
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
Noneif no layers is selected.See also
- Return type:
Optional[QgsMapLayer]
- currentLegendNode(self) QgsLayerTreeModelLegendNode | None[source]¶
Gets current legend node. May be
Noneif 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
Nonefor 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
Noneif 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
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
Noneif 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.
See also
- 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.
See also
- 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.
See also
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
nodedoes 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
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
nodedoes 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
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:
node (Optional[QgsLayerTreeNode])
expanded (bool)
- 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
layerisNonethen all layers will be deselected.See also
- Parameters:
layer (Optional[QgsMapLayer])
- setCurrentNode(self, node: QgsLayerTreeNode | None)[source]¶
Sets the currently selected
node.If
nodeisNonethen all nodes will be deselected.See also
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
- 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)