Class: QgsMapLayerModel

A model for display of map layers in widgets.

See also

QgsMapLayerProxyModel to sort and/filter the layers

See also

QgsFieldModel to combine in with a field selector.

Class Hierarchy

Inheritance diagram of qgis.core.QgsMapLayerModel

Base classes

QAbstractItemModel

QObject

Enums

CustomRole

Custom model roles.

ItemDataRole

alias of CustomRole

Methods

addLayers

additionalItems

Returns the list of additional (non map layer) items included at the end of the model.

additionalLayers

Returns the list of additional layers added to the model.

allowEmptyLayer

Returns True if the model allows the empty layer ("not set") choice.

checkAll

checkAll changes the checkstate for all the layers

indexFromLayer

indexFromLayer returns the model index for a given layer

itemsCanBeReordered

Returns True if items in the model can be reordered via drag and drop.

itemsCheckable

Returns whether the items can be checked or not

layerFromIndex

Returns the map layer corresponding to the specified index.

layersChecked

Returns the list of layers which are checked (or unchecked)

removeLayers

setAdditionalItems

Sets a list of additional (non map layer) items to include at the end of the model.

setAdditionalLayers

Sets a list of additional layers to include in the model.

setAllowEmptyLayer

Sets whether an optional empty layer ("not set") option is present in the model.

setItemsCanBeReordered

Sets whether items in the model can be reordered via drag and drop.

setItemsCheckable

Defines if layers should be selectable in the widget

setLayersChecked

Sets which layers are checked in the model.

setProject

Sets the QgsProject from which map layers are shown

setShowCrs

Sets whether the CRS of layers is also included in the model's display role.

showCrs

Returns True if the model includes layer's CRS in the display role.

Static Methods

iconForLayer

Returns the icon corresponding to a specified map layer.

class qgis.core.QgsMapLayerModel[source]

Bases: QAbstractItemModel

__init__(parent: QObject | None = None, project: QgsProject | None = None)

QgsMapLayerModel creates a model to display layers in widgets.

If project is not specified then the QgsProject.instance() project will be used to populate the model.

Parameters:
  • parent (Optional[QObject] = None)

  • project (Optional[QgsProject] = None)

__init__(layers: Iterable[QgsMapLayer], parent: QObject | None = None, project: QgsProject | None = None)

QgsMapLayerModel creates a model to display a specific list of layers in a widget.

If project is not specified then the QgsProject.instance() project will be used to populate the model.

Parameters:
class CustomRole(*values)

Bases: IntEnum

Custom model roles.

Note

Prior to QGIS 3.36 this was available as QgsMapLayerModel.ItemDataRole

Added in version 3.36.

  • LayerId: Stores the map layer ID

    Available as QgsMapLayerModel.LayerIdRole in older QGIS releases.

  • Layer: Stores pointer to the map layer itself

    Available as QgsMapLayerModel.LayerRole in older QGIS releases.

  • Empty: True if index corresponds to the empty (not set) value

    Available as QgsMapLayerModel.EmptyRole in older QGIS releases.

  • Additional: True if index corresponds to an additional (non map layer) item

    Available as QgsMapLayerModel.AdditionalRole in older QGIS releases.

ItemDataRole

alias of CustomRole

addLayers(self, layers: Iterable[QgsMapLayer])[source]
Parameters:

layers (Iterable[QgsMapLayer])

additionalItems(self) List[str][source]

Returns the list of additional (non map layer) items included at the end of the model.

Return type:

List[str]

additionalLayers(self) List[QgsMapLayer]

Returns the list of additional layers added to the model.

Added in version 3.22.

Return type:

List[QgsMapLayer]

allowEmptyLayer(self) bool[source]

Returns True if the model allows the empty layer (“not set”) choice.

Return type:

bool

checkAll(self, checkState: Qt.CheckState)[source]

checkAll changes the checkstate for all the layers

Parameters:

checkState (Qt.CheckState)

static iconForLayer(layer: QgsMapLayer | None) QIcon[source]

Returns the icon corresponding to a specified map layer.

Parameters:

layer (Optional[QgsMapLayer])

Return type:

QIcon

indexFromLayer(self, layer: QgsMapLayer | None) QModelIndex[source]

indexFromLayer returns the model index for a given layer

See also

layerFromIndex()

Parameters:

layer (Optional[QgsMapLayer])

Return type:

QModelIndex

itemsCanBeReordered(self) bool[source]

Returns True if items in the model can be reordered via drag and drop.

Added in version 3.14.

Return type:

bool

itemsCheckable(self) bool[source]

Returns whether the items can be checked or not

Return type:

bool

layerFromIndex(self, index: QModelIndex) QgsMapLayer | None[source]

Returns the map layer corresponding to the specified index.

See also

indexFromLayer()

Parameters:

index (QModelIndex)

Return type:

Optional[QgsMapLayer]

layersChecked(self, checkState: Qt.CheckState = Qt.Checked) List[QgsMapLayer]

Returns the list of layers which are checked (or unchecked)

Parameters:

checkState (Qt.CheckState = Qt.Checked)

Return type:

List[QgsMapLayer]

removeLayers(self, layerIds: Iterable[str | None])[source]
Parameters:

layerIds (Iterable[Optional[str]])

setAdditionalItems(self, items: Iterable[str | None])[source]

Sets a list of additional (non map layer) items to include at the end of the model. These may represent additional layers such as layers which are not included in the active project, or paths to layers which have not yet been loaded into QGIS.

Parameters:

items (Iterable[Optional[str]])

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

Sets a list of additional layers to include in the model.

This method allows adding additional layers, which are not part of a project’s layers, into the model.

Added in version 3.22.

Parameters:

layers (Iterable[QgsMapLayer])

setAllowEmptyLayer(self, allowEmpty: bool, text: str | None = '', icon: QIcon = QIcon())[source]

Sets whether an optional empty layer (“not set”) option is present in the model.

Since QGIS 3.20, the optional text and icon arguments allows the text and icon for the empty layer item to be set.

Parameters:
  • allowEmpty (bool)

  • text (Optional[str] = '')

  • icon (QIcon = QIcon())

setItemsCanBeReordered(self, allow: bool)[source]

Sets whether items in the model can be reordered via drag and drop.

Added in version 3.14.

Parameters:

allow (bool)

setItemsCheckable(self, checkable: bool)[source]

Defines if layers should be selectable in the widget

Parameters:

checkable (bool)

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

Sets which layers are checked in the model.

Parameters:

layers (Iterable[QgsMapLayer])

setProject(self, project: QgsProject | None)[source]

Sets the QgsProject from which map layers are shown

Added in version 3.24.

Parameters:

project (Optional[QgsProject])

setShowCrs(self, showCrs: bool)[source]

Sets whether the CRS of layers is also included in the model’s display role.

See also

showCrs()

Parameters:

showCrs (bool)

showCrs(self) bool[source]

Returns True if the model includes layer’s CRS in the display role.

See also

setShowCrs()

Return type:

bool