Class: QgsMapLayerModel

class qgis.core.QgsMapLayerModel

Bases: PyQt5.QtCore.QAbstractItemModel

The QgsMapLayerModel class is a model to display layers in widgets.

See also

QgsFieldModel

New in version 2.3.

QgsMapLayerModel(parent: QObject = None, project: QgsProject = None) QgsMapLayerModel creates a model to display layers in widgets.

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

QgsMapLayerModel(layers: Iterable[QgsMapLayer], parent: QObject = None, project: QgsProject = None) QgsMapLayerModel creates a model to display a specific list of layers in a widget.

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

Methods

addLayers

param layers:

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.

beginInsertColumns

beginInsertRows

beginMoveColumns

beginMoveRows

beginRemoveColumns

beginRemoveRows

beginResetModel

canDropMimeData

param data:

changePersistentIndex

changePersistentIndexList

checkAll

checkAll changes the checkstate for all the layers

childEvent

columnCount

param parent:

connectNotify

createIndex

customEvent

data

param index:

decodeData

disconnectNotify

dropMimeData

param data:

encodeData

endInsertColumns

endInsertRows

endMoveColumns

endMoveRows

endRemoveColumns

endRemoveRows

endResetModel

flags

param index:

iconForLayer

Returns the icon corresponding to a specified map layer.

index

param row:

indexFromLayer

indexFromLayer returns the model index for a given layer

insertRows

param row:

isSignalConnected

itemsCanBeReordered

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

itemsCheckable

returns if the items can be checked or not

layerFromIndex

Returns the map layer corresponding to the specified index.

layersChecked

layersChecked returns the list of layers which are checked (or unchecked)

mimeData

param indexes:

mimeTypes

rtype:

List[str]

parent

param child:

persistentIndexList

receivers

removeLayers

param layerIds:

removeRows

param row:

resetInternalData

rowCount

param parent:

sender

senderSignalIndex

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.

setData

param index:

setItemsCanBeReordered

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

setItemsCheckable

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.

supportedDropActions

rtype:

Qt.DropActions

timerEvent

Attributes

AdditionalRole

EmptyRole

LayerIdRole

LayerRole

AdditionalRole = 260
EmptyRole = 259
class ItemDataRole

Bases: int

baseClass

alias of QgsMapLayerModel

LayerIdRole = 257
LayerRole = 258
addLayers(self, layers: Iterable[QgsMapLayer])
Parameters:

layers (Iterable[QgsMapLayer]) –

additionalItems(self) List[str]

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

New in version 3.0.

Return type:

List[str]

additionalLayers(self) List[QgsMapLayer]

Returns the list of additional layers added to the model.

New in version 3.22.

Return type:

List[QgsMapLayer]

allowEmptyLayer(self) bool

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

New in version 3.0.

Return type:

bool

beginInsertColumns(self, QModelIndex, int, int)
beginInsertRows(self, QModelIndex, int, int)
beginMoveColumns(self, QModelIndex, int, int, QModelIndex, int) bool
beginMoveRows(self, QModelIndex, int, int, QModelIndex, int) bool
beginRemoveColumns(self, QModelIndex, int, int)
beginRemoveRows(self, QModelIndex, int, int)
beginResetModel(self)
canDropMimeData(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

changePersistentIndex(self, QModelIndex, QModelIndex)
changePersistentIndexList(self, Iterable[QModelIndex], Iterable[QModelIndex])
checkAll(self, checkState: Qt.CheckState)

checkAll changes the checkstate for all the layers

Parameters:

checkState (Qt.CheckState) –

childEvent(self, QChildEvent)
columnCount(self, parent: QModelIndex = QModelIndex()) int
Parameters:

parent (QModelIndex = QModelIndex()) –

Return type:

int

connectNotify(self, QMetaMethod)
createIndex(self, int, int, object: object = 0) QModelIndex
customEvent(self, QEvent)
data(self, index: QModelIndex, role: int = Qt.DisplayRole) Any
Parameters:
  • index (QModelIndex) –

  • role (int = Qt.DisplayRole) –

Return type:

Any

decodeData(self, int, int, QModelIndex, QDataStream) bool
disconnectNotify(self, QMetaMethod)
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(self, Iterable[QModelIndex], QDataStream)
endInsertColumns(self)
endInsertRows(self)
endMoveColumns(self)
endMoveRows(self)
endRemoveColumns(self)
endRemoveRows(self)
endResetModel(self)
flags(self, index: QModelIndex) Qt.ItemFlags
Parameters:

index (QModelIndex) –

Return type:

Qt.ItemFlags

iconForLayer(layer: QgsMapLayer) QIcon

Returns the icon corresponding to a specified map layer.

New in version 3.0.

Parameters:

layer (QgsMapLayer) –

Return type:

QIcon

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

  • column (int) –

  • parent (QModelIndex = QModelIndex()) –

Return type:

QModelIndex

indexFromLayer(self, layer: QgsMapLayer) QModelIndex

indexFromLayer returns the model index for a given layer

See also

layerFromIndex()

Parameters:

layer (QgsMapLayer) –

Return type:

QModelIndex

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

  • count (int) –

  • parent (QModelIndex = QModelIndex()) –

Return type:

bool

isSignalConnected(self, QMetaMethod) bool
itemsCanBeReordered(self) bool

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

New in version 3.14.

Return type:

bool

itemsCheckable(self) bool

returns if the items can be checked or not

Return type:

bool

layerFromIndex(self, index: QModelIndex) QgsMapLayer

Returns the map layer corresponding to the specified index.

See also

indexFromLayer()

New in version 3.0.

Parameters:

index (QModelIndex) –

Return type:

QgsMapLayer

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

layersChecked returns the list of layers which are checked (or unchecked)

Parameters:

checkState (Qt.CheckState = Qt.Checked) –

Return type:

List[QgsMapLayer]

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

indexes (Iterable[QModelIndex]) –

Return type:

QMimeData

mimeTypes(self) List[str]
Return type:

List[str]

parent(self, child: QModelIndex) QModelIndex
Parameters:

child (QModelIndex) –

Return type:

QModelIndex

persistentIndexList(self) List[QModelIndex]
receivers(self, PYQT_SIGNAL) int
removeLayers(self, layerIds: Iterable[str])
Parameters:

layerIds (Iterable[str]) –

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

  • count (int) –

  • parent (QModelIndex = QModelIndex()) –

Return type:

bool

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

parent (QModelIndex = QModelIndex()) –

Return type:

int

sender(self) QObject
senderSignalIndex(self) int
setAdditionalItems(self, items: Iterable[str])

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.

New in version 3.0.

Parameters:

items (Iterable[str]) –

setAdditionalLayers(self, layers: Iterable[QgsMapLayer])

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.

New in version 3.22.

Parameters:

layers (Iterable[QgsMapLayer]) –

setAllowEmptyLayer(self, allowEmpty: bool, text: str = '', icon: QIcon = QIcon())

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.

New in version 3.0.

Parameters:
  • allowEmpty (bool) –

  • text (str = '') –

  • icon (QIcon = QIcon()) –

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

  • value (Any) –

  • role (int = Qt.EditRole) –

Return type:

bool

setItemsCanBeReordered(self, allow: bool)

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

New in version 3.14.

Parameters:

allow (bool) –

setItemsCheckable(self, checkable: bool)

setItemsCheckable defines if layers should be selectable in the widget

Parameters:

checkable (bool) –

setLayersChecked(self, layers: Iterable[QgsMapLayer])

Sets which layers are checked in the model.

Parameters:

layers (Iterable[QgsMapLayer]) –

setProject(self, project: QgsProject)

Sets the QgsProject from which map layers are shown

New in version 3.24.

Parameters:

project (QgsProject) –

setShowCrs(self, showCrs: bool)

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

See also

showCrs()

New in version 3.0.

Parameters:

showCrs (bool) –

showCrs(self) bool

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

See also

setShowCrs()

New in version 3.0.

Return type:

bool

supportedDropActions(self) Qt.DropActions
Return type:

Qt.DropActions

timerEvent(self, QTimerEvent)