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

QgsLayerTreeViewBase

Base class for QTreeView widgets which display a layer tree.

QTreeView

QAbstractItemView

QAbstractScrollArea

QFrame

QWidget

QObject

QPaintDevice

class qgis.gui.QgsLayerTreeView[source]

Bases: QgsLayerTreeViewBase

__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:
signal contextMenuAboutToShow[source]

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.

signal currentLayerChanged[source]

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.

signal datasetsDropped[source]

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.

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

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]

layerMarkWidth(self) int[source]

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

Added in version 3.8.

Return type:

int

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]
onCurrentChanged(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:
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.

setModel(self, model: Optional[QgsLayerTreeModel], proxyModel: Optional[QgsLayerTreeProxyModel]) Sets the model and proxyModel for the view.

Use this method when a custom proxy model is required.

Added in version 4.0.

Parameters:

model (Optional[QAbstractItemModel])

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