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¶
Base classes¶
Enums
Custom model roles. |
|
alias of |
Methods
Returns the list of additional (non map layer) items included at the end of the model. |
|
Returns the list of additional layers added to the model. |
|
Returns |
|
checkAll changes the checkstate for all the layers |
|
indexFromLayer returns the model index for a given layer |
|
Returns |
|
Returns whether the items can be checked or not |
|
Returns the map layer corresponding to the specified index. |
|
Returns the list of layers which are checked (or unchecked) |
|
Sets a list of additional (non map layer) items to include at the end of the model. |
|
Sets a list of additional layers to include in the model. |
|
Sets whether an optional empty layer ("not set") option is present in the model. |
|
Sets whether items in the model can be reordered via drag and drop. |
|
Defines if layers should be selectable in the widget |
|
Sets which layers are checked in the model. |
|
Sets the |
|
Sets whether the CRS of layers is also included in the model's display role. |
|
Returns |
Static Methods
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 theQgsProject.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 theQgsProject.instance()
project will be used to populate the model.- Parameters:
layers (Iterable[QgsMapLayer])
parent (Optional[QObject] = None)
project (Optional[QgsProject] = None)
- 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 IDAvailable as
QgsMapLayerModel.LayerIdRole
in older QGIS releases.Layer
: Stores pointer to the map layer itselfAvailable as
QgsMapLayerModel.LayerRole
in older QGIS releases.Empty
: True if index corresponds to the empty (not set) valueAvailable as
QgsMapLayerModel.EmptyRole
in older QGIS releases.Additional
: True if index corresponds to an additional (non map layer) itemAvailable 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.
See also
- Return type:
List[str]
- additionalLayers(self) List[QgsMapLayer] ¶
Returns the list of additional layers added to the model.
See also
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.See also
- 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
- 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.See also
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
- 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.
See also
- 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.
See also
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
andicon
arguments allows the text and icon for the empty layer item to be set.See also
- 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.
See also
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 shownAdded 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
- Parameters:
showCrs (bool)