Class: QgsLayerTreeView

class qgis.gui.QgsLayerTreeView

Bases: PyQt5.QtWidgets.QTreeView

The QgsLayerTreeView class extends QTreeView and provides some 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(parent: QWidget = None) Constructor for QgsLayerTreeView

Methods

actionEvent

addIndicator

Adds an indicator to the given layer tree node.

changeEvent

childEvent

closeEditor

closeEvent

collapseAllNodes

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

columnCountChanged

columnMoved

columnResized

commitData

connectNotify

contextMenuEvent

param event:

create

currentChanged

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.

customEvent

defaultActions

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

destroy

dirtyRegionOffset

disconnectNotify

dragEnterEvent

param event:

dragLeaveEvent

dragMoveEvent

param event:

drawBranches

drawFrame

drawRow

drawTree

dropEvent

param event:

dropIndicatorPosition

edit

editorDestroyed

enterEvent

event

eventFilter

executeDelayedItemsLayout

expandAllNodes

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

focusInEvent

focusNextChild

focusNextPrevChild

focusOutEvent

focusPreviousChild

hideEvent

hideValidLayers

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

horizontalOffset

horizontalScrollbarAction

horizontalScrollbarValueChanged

index2legendNode

Returns legend node for given proxy model tree index.

index2node

Returns layer tree node for given proxy model tree index.

indexRowSizeHint

indicators

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

initPainter

initStyleOption

inputMethodEvent

isIndexHidden

isSignalConnected

keyPressEvent

param event:

keyReleaseEvent

layerForIndex

param index:

layerMarkWidth

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

layerTreeModel

Gets access to the model casted to QgsLayerTreeModel

leaveEvent

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.

metric

modelRowsInserted

param index:

modelRowsRemoved

mouseDoubleClickEvent

param event:

mouseMoveEvent

mousePressEvent

mouseReleaseEvent

param event:

moveCursor

moveEvent

nativeEvent

node2index

Returns proxy model index for a given node.

node2sourceIndex

Returns source model index for a given node.

onCurrentChanged

onExpandedChanged

param node:

onModelReset

paintEvent

proxyModel

Returns the proxy model used by the view.

receivers

reexpand

refreshLayerSymbology

Force refresh of layer symbology.

removeIndicator

Removes a previously added indicator to a layer tree node.

resizeEvent

param event:

rowHeight

rowsAboutToBeRemoved

rowsInserted

rowsRemoved

scheduleDelayedItemsLayout

scrollContentsBy

scrollDirtyRegion

selectedIndexes

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.

selectionChanged

selectionCommand

sender

senderSignalIndex

setCurrentLayer

Sets the currently selected layer.

setDirtyRegion

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

setModel

Overridden setModel() from base class.

setSelection

setShowPrivateLayers

Set the show private layers to showPrivate

setState

setViewportMargins

sharedPainter

showEvent

showPrivateLayers

Returns the show private layers status

sizeHintForColumn

startDrag

state

tabletEvent

timerEvent

updateEditorData

updateEditorGeometries

updateExpandedStateFromNode

param node:

updateExpandedStateToNode

param index:

updateGeometries

updateMicroFocus

verticalOffset

verticalScrollbarAction

verticalScrollbarValueChanged

viewOptions

viewportEvent

viewportMargins

viewportSizeHint

visualRegionForSelection

wheelEvent

Signals

contextMenuAboutToShow

pyqtSignal(*types, name: str = ..., revision: int = ..., arguments: Sequence = ...) -> PYQT_SIGNAL

currentLayerChanged

pyqtSignal(*types, name: str = ..., revision: int = ..., arguments: Sequence = ...) -> PYQT_SIGNAL

datasetsDropped

pyqtSignal(*types, name: str = ..., revision: int = ..., arguments: Sequence = ...) -> PYQT_SIGNAL

actionEvent(self, QActionEvent)
addIndicator(self, node: QgsLayerTreeNode, indicator: QgsLayerTreeViewIndicator)

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()

New in version 3.2.

Parameters:
changeEvent(self, QEvent)
childEvent(self, QChildEvent)
closeEditor(self, QWidget, QAbstractItemDelegate.EndEditHint)
closeEvent(self, QCloseEvent)
collapseAllNodes(self)

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

columnCountChanged(self, int, int)
columnMoved(self)
columnResized(self, int, int, int)
commitData(self, QWidget)
connectNotify(self, QMetaMethod)
contextMenuAboutToShow

pyqtSignal(*types, name: str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

Parameters:
  • name (str = ...) –

  • revision (int = ...) –

  • arguments (Sequence = ...) –

Return type:

PYQT_SIGNAL

contextMenuEvent(self, event: QContextMenuEvent)
Parameters:

event (QContextMenuEvent) –

create(self, window: PyQt5.sip.voidptr = 0, initializeWindow: bool = True, destroyOldWindow: bool = True)
currentChanged(self, QModelIndex, QModelIndex)
currentGroupNode(self) QgsLayerTreeGroup

Gets current group node. If a layer is current node, the function will return parent group. May be None.

Return type:

QgsLayerTreeGroup

currentLayer(self) QgsMapLayer

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

Return type:

QgsMapLayer

currentLayerChanged

pyqtSignal(*types, name: str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

Parameters:
  • name (str = ...) –

  • revision (int = ...) –

  • arguments (Sequence = ...) –

Return type:

PYQT_SIGNAL

currentLegendNode(self) QgsLayerTreeModelLegendNode

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

Return type:

QgsLayerTreeModelLegendNode

currentNode(self) QgsLayerTreeNode

Gets current node. May be None

Return type:

QgsLayerTreeNode

customEvent(self, QEvent)
datasetsDropped

pyqtSignal(*types, name: str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL

types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.

Parameters:
  • name (str = ...) –

  • revision (int = ...) –

  • arguments (Sequence = ...) –

Return type:

PYQT_SIGNAL

defaultActions(self) QgsLayerTreeViewDefaultActions

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

Return type:

QgsLayerTreeViewDefaultActions

destroy(self, destroyWindow: bool = True, destroySubWindows: bool = True)
dirtyRegionOffset(self) QPoint
disconnectNotify(self, QMetaMethod)
dragEnterEvent(self, event: QDragEnterEvent)
Parameters:

event (QDragEnterEvent) –

dragLeaveEvent(self, QDragLeaveEvent)
dragMoveEvent(self, event: QDragMoveEvent)
Parameters:

event (QDragMoveEvent) –

drawBranches(self, QPainter, QRect, QModelIndex)
drawFrame(self, QPainter)
drawRow(self, QPainter, QStyleOptionViewItem, QModelIndex)
drawTree(self, QPainter, QRegion)
dropEvent(self, event: QDropEvent)
Parameters:

event (QDropEvent) –

dropIndicatorPosition(self) QAbstractItemView.DropIndicatorPosition
edit(self, QModelIndex)
edit(self, QModelIndex, QAbstractItemView.EditTrigger, QEvent) bool
editorDestroyed(self, QObject)
enterEvent(self, QEvent)
event(self, QEvent) bool
eventFilter(self, QObject, QEvent) bool
executeDelayedItemsLayout(self)
expandAllNodes(self)

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

focusInEvent(self, QFocusEvent)
focusNextChild(self) bool
focusNextPrevChild(self, bool) bool
focusOutEvent(self, QFocusEvent)
focusPreviousChild(self) bool
hideEvent(self, QHideEvent)
hideValidLayers(self) bool

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

New in version 3.38.

Return type:

bool

horizontalOffset(self) int
horizontalScrollbarAction(self, int)
horizontalScrollbarValueChanged(self, int)
index2legendNode(self, index: QModelIndex) QgsLayerTreeModelLegendNode

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.

New in version 3.18.

Parameters:

index (QModelIndex) –

Return type:

QgsLayerTreeModelLegendNode

index2node(self, index: QModelIndex) QgsLayerTreeNode

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.

New in version 3.18.

Parameters:

index (QModelIndex) –

Return type:

QgsLayerTreeNode

indexRowSizeHint(self, QModelIndex) int
indicators(self, node: QgsLayerTreeNode) List[QgsLayerTreeViewIndicator]

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

See also

addIndicator()

New in version 3.2.

Parameters:

node (QgsLayerTreeNode) –

Return type:

List[QgsLayerTreeViewIndicator]

initPainter(self, QPainter)
initStyleOption(self, QStyleOptionFrame)
inputMethodEvent(self, QInputMethodEvent)
isIndexHidden(self, QModelIndex) bool
isSignalConnected(self, QMetaMethod) bool
keyPressEvent(self, event: QKeyEvent)
Parameters:

event (QKeyEvent) –

keyReleaseEvent(self, QKeyEvent)
layerForIndex(self, index: QModelIndex) QgsMapLayer
Parameters:

index (QModelIndex) –

Return type:

QgsMapLayer

layerMarkWidth(self) int

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

New in version 3.8.

Return type:

int

layerTreeModel(self) QgsLayerTreeModel

Gets access to the model casted to QgsLayerTreeModel

Return type:

QgsLayerTreeModel

leaveEvent(self, QEvent)
legendNode2index(self, legendNode: QgsLayerTreeModelLegendNode) QModelIndex

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.

New in version 3.18.

Parameters:

legendNode (QgsLayerTreeModelLegendNode) –

Return type:

QModelIndex

legendNode2sourceIndex(self, legendNode: QgsLayerTreeModelLegendNode) QModelIndex

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.

New in version 3.18.

Parameters:

legendNode (QgsLayerTreeModelLegendNode) –

Return type:

QModelIndex

menuProvider(self) QgsLayerTreeViewMenuProvider

Returns pointer to the context menu provider. May be None

Return type:

QgsLayerTreeViewMenuProvider

metric(self, QPaintDevice.PaintDeviceMetric) int
modelRowsInserted(self, index: QModelIndex, start: int, end: int)
Parameters:
  • index (QModelIndex) –

  • start (int) –

  • end (int) –

modelRowsRemoved(self)
mouseDoubleClickEvent(self, event: QMouseEvent)
Parameters:

event (QMouseEvent) –

mouseMoveEvent(self, QMouseEvent)
mousePressEvent(self, QMouseEvent)
mouseReleaseEvent(self, event: QMouseEvent)
Parameters:

event (QMouseEvent) –

moveCursor(self, QAbstractItemView.CursorAction, Union[Qt.KeyboardModifiers, Qt.KeyboardModifier]) QModelIndex
moveEvent(self, QMoveEvent)
nativeEvent(self, Union[QByteArray, bytes, bytearray], PyQt5.sip.voidptr) Tuple[bool, int]
node2index(self, node: QgsLayerTreeNode) QModelIndex

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.

New in version 3.18.

Parameters:

node (QgsLayerTreeNode) –

Return type:

QModelIndex

node2sourceIndex(self, node: QgsLayerTreeNode) QModelIndex

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

New in version 3.18.

Parameters:

node (QgsLayerTreeNode) –

Return type:

QModelIndex

onCurrentChanged(self)
onExpandedChanged(self, node: QgsLayerTreeNode, expanded: bool)
Parameters:
onModelReset(self)
paintEvent(self, QPaintEvent)
proxyModel(self) QgsLayerTreeProxyModel

Returns the proxy model used by the view.

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

New in version 3.18.

Return type:

QgsLayerTreeProxyModel

receivers(self, PYQT_SIGNAL) int
reexpand(self)
refreshLayerSymbology(self, layerId: str)

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

Parameters:

layerId (str) –

removeIndicator(self, node: QgsLayerTreeNode, indicator: QgsLayerTreeViewIndicator)

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

See also

addIndicator()

See also

indicators()

New in version 3.2.

Parameters:
resizeEvent(self, event: QResizeEvent)
Parameters:

event (QResizeEvent) –

rowHeight(self, QModelIndex) int
rowsAboutToBeRemoved(self, QModelIndex, int, int)
rowsInserted(self, QModelIndex, int, int)
rowsRemoved(self, QModelIndex, int, int)
scheduleDelayedItemsLayout(self)
scrollContentsBy(self, int, int)
scrollDirtyRegion(self, int, int)
selectedIndexes(self) List[QModelIndex]
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.

New in version 3.4.

Return type:

List[QgsMapLayer]

selectedLegendNodes(self) List[QgsLayerTreeModelLegendNode]

Returns the list of selected legend nodes.

See also

selectedNodes()

New 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]

selectionChanged(self, QItemSelection, QItemSelection)
selectionCommand(self, QModelIndex, event: QEvent = None) QItemSelectionModel.SelectionFlags
sender(self) QObject
senderSignalIndex(self) int
setCurrentLayer(self, layer: QgsMapLayer)

Sets the currently selected layer.

If layer is None then all layers will be deselected.

See also

currentLayer()

Parameters:

layer (QgsMapLayer) –

setDirtyRegion(self, QRegion)
setHideValidLayers(self, hideValid: bool)

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

New in version 3.38.

Parameters:

hideValid (bool) –

setLayerMarkWidth(self, width: int)

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

See also

layerMarkWidth()

New in version 3.8.

Parameters:

width (int) –

setLayerVisible(self, layer: QgsMapLayer, visible: bool)

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

See also

QgsLayerTreeNode.setItemVisibilityChecked()

New in version 3.10.

Parameters:
setMenuProvider(self, menuProvider: QgsLayerTreeViewMenuProvider)

Sets provider for context menu. Takes ownership of the instance

Parameters:

menuProvider (QgsLayerTreeViewMenuProvider) –

setMessageBar(self, messageBar: QgsMessageBar)

Set the message bar to display messages from the layer tree

New in version 3.14.

Parameters:

messageBar (QgsMessageBar) –

setModel(self, model: QAbstractItemModel)

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

Parameters:

model (QAbstractItemModel) –

setSelection(self, QRect, Union[QItemSelectionModel.SelectionFlags, QItemSelectionModel.SelectionFlag])
setShowPrivateLayers(self, showPrivate: bool)

Set the show private layers to showPrivate

New in version 3.18.

Parameters:

showPrivate (bool) –

setState(self, QAbstractItemView.State)
setViewportMargins(self, int, int, int, int)
setViewportMargins(self, QMargins) None
sharedPainter(self) QPainter
showEvent(self, QShowEvent)
showPrivateLayers(self) bool

Returns the show private layers status

New in version 3.18.

Return type:

bool

sizeHintForColumn(self, int) int
startDrag(self, Union[Qt.DropActions, Qt.DropAction])
state(self) QAbstractItemView.State
tabletEvent(self, QTabletEvent)
timerEvent(self, QTimerEvent)
updateEditorData(self)
updateEditorGeometries(self)
updateExpandedStateFromNode(self, node: QgsLayerTreeNode)
Parameters:

node (QgsLayerTreeNode) –

updateExpandedStateToNode(self, index: QModelIndex)
Parameters:

index (QModelIndex) –

updateGeometries(self)
updateMicroFocus(self)
verticalOffset(self) int
verticalScrollbarAction(self, int)
verticalScrollbarValueChanged(self, int)
viewOptions(self) QStyleOptionViewItem
viewportEvent(self, QEvent) bool
viewportMargins(self) QMargins
viewportSizeHint(self) QSize
visualRegionForSelection(self, QItemSelection) QRegion
wheelEvent(self, QWheelEvent)