Class: QgsLayerTreeModel

class qgis.core.QgsLayerTreeModel(rootNode: QgsLayerTree, parent: QObject = None)

Bases: PyQt5.QtCore.QAbstractItemModel

Construct a new tree model with given layer tree (root node must not be None). The root node is not transferred by the model.

The QgsLayerTreeModel class is model implementation for Qt item views framework. The model can be used in any QTreeView, it is however recommended to use it with QgsLayerTreeView which brings additional functionality specific to layer tree handling.

The model listens to the changes in the layer tree and signals the changes as appropriate, so that any view that uses the model is updated accordingly.

Behavior of the model can be customized with flags. For example, whether to show legend or whether to allow changes to the layer tree.

See also

QgsLayerTreeView

New in version 2.4.

Parameters
ActionHierarchical = 65536
AllowLegendChangeState = 32768
AllowNodeChangeVisibility = 16384
AllowNodeRename = 8192
AllowNodeReorder = 4096
DeferredLegendInvalidation = 8
class Flag

Bases: int

class Flags

Bases: sip.wrapper

QgsLayerTreeModel.Flags(Union[QgsLayerTreeModel.Flags, QgsLayerTreeModel.Flag]) QgsLayerTreeModel.Flags(QgsLayerTreeModel.Flags)

ShowLegend = 1
ShowLegendAsTree = 4
UseEmbeddedWidgets = 16
UseTextFormatting = 32
addLegendToLayer(self, nodeL: QgsLayerTreeLayer)
Parameters

nodeL (QgsLayerTreeLayer) –

autoCollapseLegendNodes(self) → int

Returns at what number of legend nodes the layer node should be collapsed. -1 means no auto-collapse (default).

Return type

int

beginInsertColumns()
beginInsertRows()
beginMoveColumns()
beginMoveRows()
beginRemoveColumns()
beginRemoveRows()
beginResetModel()
changePersistentIndex()
changePersistentIndexList()
childEvent()
columnCount(self, parent: QModelIndex = QModelIndex()) → int
Parameters

parent (QModelIndex = QModelIndex()) –

Return type

int

connectNotify()
connectToLayer(self, nodeLayer: QgsLayerTreeLayer)
Parameters

nodeLayer (QgsLayerTreeLayer) –

connectToLayers(self, parentGroup: QgsLayerTreeGroup)
Parameters

parentGroup (QgsLayerTreeGroup) –

connectToRootNode(self)
createIndex()
currentIndex(self) → QModelIndex

Gets index of the item marked as current. Item marked as current is underlined.

Return type

QModelIndex

customEvent()
data(self, index: QModelIndex, role: int = Qt.DisplayRole) → Any
Parameters
  • index (QModelIndex) –

  • role (int = Qt.DisplayRole) –

Return type

Any

decodeData()
disconnectFromLayer(self, nodeLayer: QgsLayerTreeLayer)
Parameters

nodeLayer (QgsLayerTreeLayer) –

disconnectFromLayers(self, parentGroup: QgsLayerTreeGroup)
Parameters

parentGroup (QgsLayerTreeGroup) –

disconnectFromRootNode(self)
disconnectNotify()
dropMimeData(self, data: QMimeData, action: Qt.DropAction, row: int, column: int, parent: QModelIndex) → bool
Parameters
  • data (QMimeData) –

  • action (Qt.DropAction) –

  • row (int) –

  • column (int) –

  • parent (QModelIndex) –

Return type

bool

encodeData()
endInsertColumns()
endInsertRows()
endMoveColumns()
endMoveRows()
endRemoveColumns()
endRemoveRows()
endResetModel()
filterLegendNodes(self, nodes: Iterable[QgsLayerTreeModelLegendNode]) → List[QgsLayerTreeModelLegendNode]

Filter nodes from QgsMapLayerLegend according to the current filtering rules

Parameters

nodes (Iterable[QgsLayerTreeModelLegendNode]) –

Return type

List[QgsLayerTreeModelLegendNode]

findLegendNode(self, layerId: str, ruleKey: str) → QgsLayerTreeModelLegendNode

Searches through the layer tree to find a legend node with a matching layer ID and rule key.

Parameters
  • layerId (str) – map layer ID

  • ruleKey (str) – legend node rule key

Return type

QgsLayerTreeModelLegendNode

Returns

QgsLayerTreeModelLegendNode if found

New in version 2.14.

flags(self, index: QModelIndex) → Qt.ItemFlags

flags(self) -> QgsLayerTreeModel.Flags Returns OR-ed combination of model flags

Parameters

index (QModelIndex) –

Return type

Qt.ItemFlags

iconGroup() → QIcon
Return type

QIcon

index(self, row: int, column: int, parent: QModelIndex = QModelIndex()) → QModelIndex
Parameters
  • row (int) –

  • column (int) –

  • parent (QModelIndex = QModelIndex()) –

Return type

QModelIndex

index2legendNode(index: QModelIndex) → QgsLayerTreeModelLegendNode

Returns legend node for given index. Returns None for invalid index

New in version 2.6.

Parameters

index (QModelIndex) –

Return type

QgsLayerTreeModelLegendNode

index2node(self, index: QModelIndex) → QgsLayerTreeNode

Returns layer tree node for given 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)

Parameters

index (QModelIndex) –

Return type

QgsLayerTreeNode

indexOfParentLayerTreeNode(self, parentNode: QgsLayerTreeNode) → QModelIndex
Parameters

parentNode (QgsLayerTreeNode) –

Return type

QModelIndex

indexes2nodes(self, list: Iterable[QModelIndex], skipInternal: bool = False) → List[QgsLayerTreeNode]

Convert a list of indexes to a list of layer tree nodes. Indices that do not represent layer tree nodes are skipped. If skipInternal is True, a node is included in the output list only if no parent node is in the list.

Parameters
  • list (Iterable[QModelIndex]) –

  • skipInternal (bool = False) –

Return type

List[QgsLayerTreeNode]

invalidateLegendMapBasedData(self)
isSignalConnected()
layerLegendChanged(self)
layerLegendNodes(self, nodeLayer: QgsLayerTreeLayer, skipNodeEmbeddedInParent: bool = False) → List[QgsLayerTreeModelLegendNode]

Returns filtered list of active legend nodes attached to a particular layer node (by default it returns also legend node embedded in parent layer node (if any) unless skipNodeEmbeddedInParent is True)

Note

Parameter skipNodeEmbeddedInParent added in QGIS 2.18

New in version 2.6.

Parameters
Return type

List[QgsLayerTreeModelLegendNode]

layerNeedsUpdate(self)
layerOriginalLegendNodes(self, nodeLayer: QgsLayerTreeLayer) → List[QgsLayerTreeModelLegendNode]

Returns original (unfiltered) list of legend nodes attached to a particular layer node

New in version 2.14.

Parameters

nodeLayer (QgsLayerTreeLayer) –

Return type

List[QgsLayerTreeModelLegendNode]

layerStyleOverrides(self) → Dict[str, str]

Gets map of map layer style overrides (key: layer ID, value: style name) where a different style should be used instead of the current one

New in version 2.10.

Return type

Dict[str, str]

layerTreeNodeFont(self, nodeType: int) → QFont

Gets font for a particular type of layer tree node. nodeType should come from QgsLayerTreeNode.NodeType enumeration

Parameters

nodeType (int) –

Return type

QFont

legendCleanup(self)
legendEmbeddedInParent(self, nodeLayer: QgsLayerTreeLayer) → bool
Parameters

nodeLayer (QgsLayerTreeLayer) –

Return type

bool

legendFilterByScale(self) → float

Returns the scale which restricts the legend nodes which are visible. The scale value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map. A scale <= 0 indicates that no scale filtering is being performed.

New in version 2.6.

Return type

float

legendFilterMapSettings(self) → QgsMapSettings

Returns the current map settings used for the current legend filter (or None if none is enabled)

New in version 2.14.

Return type

QgsMapSettings

legendIconEmbeddedInParent(self, nodeLayer: QgsLayerTreeLayer) → QIcon
Parameters

nodeLayer (QgsLayerTreeLayer) –

Return type

QIcon

legendInvalidateMapBasedData(self)
legendMapViewData(self) → Tuple[float, int, float]

Gets hints about map view - to be used in legend nodes. Arguments that are not None will receive values. If there are no valid map view data (from previous call to setLegendMapViewData()), returned values are zeros.

New in version 2.6.

Return type

Tuple[float, int, float]

legendNode2index(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 2.6.

Parameters

legendNode (QgsLayerTreeModelLegendNode) –

Return type

QModelIndex

legendNodeData(self, node: QgsLayerTreeModelLegendNode, role: int) → Any
Parameters
Return type

Any

legendNodeDataChanged(self)
legendNodeEmbeddedInParent(self, nodeLayer: QgsLayerTreeLayer) → QgsLayerTreeModelLegendNode

Returns legend node that may be embedded in parent (i.e. its icon will be used for layer’s icon).

New in version 2.18.

Parameters

nodeLayer (QgsLayerTreeLayer) –

Return type

QgsLayerTreeModelLegendNode

legendNodeFlags(self, node: QgsLayerTreeModelLegendNode) → Qt.ItemFlags
Parameters

node (QgsLayerTreeModelLegendNode) –

Return type

Qt.ItemFlags

legendNodeIndex(self, row: int, column: int, node: QgsLayerTreeModelLegendNode) → QModelIndex
Parameters
Return type

QModelIndex

legendNodeRowCount(self, node: QgsLayerTreeModelLegendNode) → int
Parameters

node (QgsLayerTreeModelLegendNode) –

Return type

int

legendParent(self, legendNode: QgsLayerTreeModelLegendNode) → QModelIndex
Parameters

legendNode (QgsLayerTreeModelLegendNode) –

Return type

QModelIndex

legendRootIndex(self, row: int, column: int, nL: QgsLayerTreeLayer) → QModelIndex
Parameters
Return type

QModelIndex

legendRootRowCount(self, nL: QgsLayerTreeLayer) → int
Parameters

nL (QgsLayerTreeLayer) –

Return type

int

mimeData(self, indexes: Iterable[QModelIndex]) → QMimeData
Parameters

indexes (Iterable[QModelIndex]) –

Return type

QMimeData

mimeTypes(self) → List[str]
Return type

List[str]

node2index(self, node: QgsLayerTreeNode) → QModelIndex

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

Parameters

node (QgsLayerTreeNode) –

Return type

QModelIndex

nodeAddedChildren(self, node: QgsLayerTreeNode, indexFrom: int, indexTo: int)
Parameters
nodeCustomPropertyChanged(self, node: QgsLayerTreeNode, key: str)
Parameters
nodeLayerLoaded(self)
nodeLayerWillBeUnloaded(self)
nodeNameChanged(self, node: QgsLayerTreeNode, name: str)

Updates model when node’s name has changed

New in version 3.0.

Parameters
nodeRemovedChildren(self)
nodeVisibilityChanged(self, node: QgsLayerTreeNode)
Parameters

node (QgsLayerTreeNode) –

nodeWillAddChildren(self, node: QgsLayerTreeNode, indexFrom: int, indexTo: int)
Parameters
nodeWillRemoveChildren(self, node: QgsLayerTreeNode, indexFrom: int, indexTo: int)
Parameters
parent(self, child: QModelIndex) → QModelIndex
Parameters

child (QModelIndex) –

Return type

QModelIndex

persistentIndexList()
receivers()
recursivelyEmitDataChanged(self, index: QModelIndex = QModelIndex())

emit dataChanged() for layer tree node items

Parameters

index (QModelIndex = QModelIndex()) –

refreshLayerLegend(self, nodeLayer: QgsLayerTreeLayer)

Force a refresh of legend nodes of a layer node. Not necessary to call when layer’s renderer is changed as the model listens to these events.

Parameters

nodeLayer (QgsLayerTreeLayer) –

refreshScaleBasedLayers(self, index: QModelIndex = QModelIndex())

Updates layer data for scale dependent layers, should be called when map scale changes. Emits dataChanged() for all scale dependent layers.

New in version 2.16.

Parameters

index (QModelIndex = QModelIndex()) –

removeLegendFromLayer(self, nodeLayer: QgsLayerTreeLayer)
Parameters

nodeLayer (QgsLayerTreeLayer) –

removeRows(self, row: int, count: int, parent: QModelIndex = QModelIndex()) → bool
Parameters
  • row (int) –

  • count (int) –

  • parent (QModelIndex = QModelIndex()) –

Return type

bool

resetInternalData()
rootGroup(self) → QgsLayerTree

Returns pointer to the root node of the layer tree. Always a non None value.

Return type

QgsLayerTree

rowCount(self, parent: QModelIndex = QModelIndex()) → int
Parameters

parent (QModelIndex = QModelIndex()) –

Return type

int

scaleIconSize(standardSize: int) → int

Scales an layer tree model icon size to compensate for display pixel density, making the icon size hi-dpi friendly, whilst still resulting in pixel-perfect sizes for low-dpi displays.

standardSize should be set to a standard icon size, e.g. 16, 24, 48, etc.

New in version 3.6.

Parameters

standardSize (int) –

Return type

int

sender()
senderSignalIndex()
setAutoCollapseLegendNodes(self, nodeCount: int)

Sets at what number of legend nodes the layer node should be collapsed. Setting -1 disables the auto-collapse (default).

Parameters

nodeCount (int) –

setCurrentIndex(self, currentIndex: QModelIndex)

Sets index of the current item. May be used by view. Item marked as current is underlined.

Parameters

currentIndex (QModelIndex) –

setData(self, index: QModelIndex, value: Any, role: int = Qt.EditRole) → bool
Parameters
  • index (QModelIndex) –

  • value (Any) –

  • role (int = Qt.EditRole) –

Return type

bool

setFlag(self, f: QgsLayerTreeModel.Flag, on: bool = True)

Enable or disable a model flag

Parameters
setFlags(self, f: Union[QgsLayerTreeModel.Flags, QgsLayerTreeModel.Flag])

Sets OR-ed combination of model flags

Parameters

f (Union[QgsLayerTreeModel.Flags) –

setLayerStyleOverrides(self, overrides: Dict[str, str])

Sets map of map layer style overrides (key: layer ID, value: style name) where a different style should be used instead of the current one

New in version 2.10.

Parameters

overrides (Dict[str) –

setLayerTreeNodeFont(self, nodeType: int, font: QFont)

Sets font for a particular type of layer tree node. nodeType should come from QgsLayerTreeNode.NodeType enumeration

Parameters
  • nodeType (int) –

  • font (QFont) –

setLegendFilter(self, settings: QgsMapSettings, useExtent: bool = True, polygon: QgsGeometry = QgsGeometry(), useExpressions: bool = True)

Filter display of legend nodes for given map settings

Parameters
  • settings (QgsMapSettings) – Map settings. Setting None or invalid settings will disable any filter. Ownership is not changed, a copy is made

  • useExtent (bool = True) – Whether to use the extent of the map settings as a first spatial filter on legend nodes

  • polygon (QgsGeometry = QgsGeometry()) – If not empty, this polygon will be used instead of the map extent to filter legend nodes

  • useExpressions (bool = True) – Whether to use legend node filter expressions

New in version 2.14.

setLegendFilterByMap(self, settings: QgsMapSettings)

Force only display of legend nodes which are valid for given map settings. Setting None or invalid map settings will disable the functionality. Ownership of map settings pointer does not change, a copy is made.

New in version 2.6.

Parameters

settings (QgsMapSettings) –

setLegendFilterByScale(self, scale: float)

Force only display of legend nodes which are valid for a given scale. The scale value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map. Setting scale <= 0 will disable the functionality.

New in version 2.6.

Parameters

scale (float) –

setLegendMapViewData(self, mapUnitsPerPixel: float, dpi: int, scale: float)

Give the layer tree model hints about the currently associated map view so that legend nodes that use map units can be scaled correctly

New in version 2.6.

Parameters
  • mapUnitsPerPixel (float) –

  • dpi (int) –

  • scale (float) –

setRootGroup(self, newRootGroup: QgsLayerTree)

Reset the model and use a new root group node

New in version 2.6.

Parameters

newRootGroup (QgsLayerTree) –

supportedDropActions(self) → Qt.DropActions
Return type

Qt.DropActions

testFlag(self, f: QgsLayerTreeModel.Flag) → bool

Check whether a flag is enabled

Parameters

f (QgsLayerTreeModel.Flag) –

Return type

bool

timerEvent()