Class: QgsLayerTreeFilterProxyModel

QgsLayerTreeFilterProxyModel is a sort filter proxy model to easily reproduce the legend/layer tree in a tree view.

Layers are checkable by default. Symbology nodes will not be shown. Layers can be filtered by their type.

For more complex use-cases, the model can be re-implemented to allow a different interaction or to add more columns.

Added in version 3.14.

Class Hierarchy

Inheritance diagram of qgis.core.QgsLayerTreeFilterProxyModel

Base classes

QSortFilterProxyModel

QAbstractProxyModel

QAbstractItemModel

QObject

Methods

checkedLayers

Returns the checked layers

isLayerChecked

Returns if the layer is checked or not

layerTreeModel

Rerturns the layer tree model

mapLayer

Returns the map layer at a given index

nodeShown

Returns True if the specified node will be shown in the model.

setCheckedLayers

Initialize the list of checked layers.

setFilterText

Sets the filter text to search for a layer in the tree

setFilters

Defines the type layers (vector, raster, etc) shown in the tree If the list is empty, all types are shown.

setLayerChecked

This will set if the layer is checked or not

setLayerTreeModel

Sets the layer tree model

setShowPrivateLayers

Determines if private layers are shown.

showPrivateLayers

Returns if private layers are shown.

class qgis.core.QgsLayerTreeFilterProxyModel[source]

Bases: QSortFilterProxyModel

__init__(parent: QObject | None = None)

Constructor

Parameters:

parent (Optional[QObject] = None)

checkedLayers(self) List[QgsMapLayer]

Returns the checked layers

Return type:

List[QgsMapLayer]

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

Returns if the layer is checked or not

Parameters:

layer (Optional[QgsMapLayer])

Return type:

bool

layerTreeModel(self) QgsLayerTreeModel | None[source]

Rerturns the layer tree model

Return type:

Optional[QgsLayerTreeModel]

mapLayer(self, idx: QModelIndex) QgsMapLayer | None[source]

Returns the map layer at a given index

Parameters:

idx (QModelIndex)

Return type:

Optional[QgsMapLayer]

nodeShown(self, node: QgsLayerTreeNode | None) bool[source]

Returns True if the specified node will be shown in the model.

Added in version 3.40.

Parameters:

node (Optional[QgsLayerTreeNode])

Return type:

bool

setCheckedLayers(self, layers: Iterable[QgsMapLayer])[source]

Initialize the list of checked layers.

Note

If the model is re-implemented, this method might become useless

Parameters:

layers (Iterable[QgsMapLayer])

setFilterText(self, filterText: str | None = '')[source]

Sets the filter text to search for a layer in the tree

Parameters:

filterText (Optional[str] = '')

setFilters(self, filters: Qgis.LayerFilters | Qgis.LayerFilter)[source]

Defines the type layers (vector, raster, etc) shown in the tree If the list is empty, all types are shown.

Parameters:

filters (Union[Qgis.LayerFilters, Qgis.LayerFilter])

setLayerChecked(self, layer: QgsMapLayer | None, checked: bool)[source]

This will set if the layer is checked or not

Parameters:
setLayerTreeModel(self, layerTreeModel: QgsLayerTreeModel | None)[source]

Sets the layer tree model

Parameters:

layerTreeModel (Optional[QgsLayerTreeModel])

setShowPrivateLayers(self, showPrivate: bool)[source]

Determines if private layers are shown.

Defaults to True.

Added in version 3.40.

Parameters:

showPrivate (bool)

showPrivateLayers(self) bool[source]

Returns if private layers are shown.

Defaults to True.

Added in version 3.40.

Return type:

bool