Class: QgsMapLayerStore

A storage object for map layers, in which the layers are owned by the store and have their lifetime bound to the store.

Class Hierarchy

Inheritance diagram of qgis.core.QgsMapLayerStore

Base classes

QObject

Methods

addMapLayer

Add a layer to the store.

addMapLayers

Add a list of layers to the store.

count

Returns the number of layers contained in the store.

mapLayer

Retrieve a pointer to a layer by layer id.

mapLayers

Returns a map of all layers by layer ID.

mapLayersByName

Retrieve a list of matching layers by layer name.

removeAllMapLayers

Removes all registered layers.

removeMapLayer

Remove a layer from the store by layer id.

removeMapLayers

Remove a set of layers from the store.

removeMapLayersById

Remove a set of layers from the store by layer ID.

takeMapLayer

Takes a layer from the store.

transferLayersFromStore

Transfers all the map layers contained within another map layer store and adds them to this store.

validCount

Returns the number of valid layers contained in the store.

validMapLayers

Returns a map of all valid layers by layer ID.

Signals

allLayersRemoved

Emitted when all layers are removed, before layersWillBeRemoved() and layerWillBeRemoved() signals are emitted.

layerRemoved

Emitted after a layer was removed from the store.

layerWasAdded

Emitted when a layer was added to the store.

layerWillBeRemoved

Emitted when a layer is about to be removed from the store.

layersAdded

Emitted when one or more layers were added to the store.

layersRemoved

Emitted after one or more layers were removed from the store.

layersWillBeRemoved

Emitted when one or more layers are about to be removed from the store.

class qgis.core.QgsMapLayerStore[source]

Bases: QObject

__init__(parent: QObject | None = None)

Constructor for QgsMapLayerStore.

Parameters:

parent (Optional[QObject] = None)

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

Add a layer to the store. Ownership of the layer is transferred to the store.

The layersAdded() and layerWasAdded() signals will always be emitted. If you are adding multiple layers at once, you should use addMapLayers() instead.

Parameters:

layer (Optional[QgsMapLayer]) – A layer to add to the store

Return type:

Optional[QgsMapLayer]

Returns:

None if unable to add layer, otherwise pointer to newly added layer

See also

addMapLayers()

Note

Use addMapLayers() if adding more than one layer at a time.

See also

addMapLayers()

addMapLayers(self, layers: Iterable[QgsMapLayer]) List[QgsMapLayer]

Add a list of layers to the store. Ownership of the layers is transferred to the store.

The layersAdded() and layerWasAdded() signals will always be emitted.

Parameters:

layers (Iterable[QgsMapLayer]) – A list of layer which should be added to the store.

Note

If a layer with the same id is already in the store it is not added again, but if the validity of the layer has changed from False to True, the layer data source is updated to the new one.

Return type:

List[QgsMapLayer]

Returns:

a list of the map layers that were added successfully. If a layer already exists in the store, it will not be part of the returned list.

See also

addMapLayer()

signal allLayersRemoved[source]

Emitted when all layers are removed, before layersWillBeRemoved() and layerWillBeRemoved() signals are emitted. The layersWillBeRemoved() and layerWillBeRemoved() signals will still be emitted following this signal. You can use this signal to do easy (and fast) cleanup.

count(self) int[source]

Returns the number of layers contained in the store.

Return type:

int

signal layerRemoved(layerId: str)[source]

Emitted after a layer was removed from the store.

Parameters:

layerId (str) – The ID of the layer removed.

Note

Consider using layersRemoved() instead

signal layerWasAdded(layer: QgsMapLayer)[source]

Emitted when a layer was added to the store.

Note

Consider using layersAdded() instead

See also

layersAdded()

Parameters:

layer (QgsMapLayer)

signal layerWillBeRemoved(layer: QgsMapLayer)[source]

Emitted when a layer is about to be removed from the store.

Parameters:

layer (QgsMapLayer) – The layer to be removed.

Note

Consider using layersWillBeRemoved() instead.

See also

layerRemoved()

signal layersAdded(layers: List[QgsMapLayer])[source]

Emitted when one or more layers were added to the store.

Parameters:

layers (List[QgsMapLayer]) – List of layers which have been added.

See also

layerWasAdded()

signal layersRemoved(layerIds: List[str])[source]

Emitted after one or more layers were removed from the store.

Parameters:

layerIds (List[str]) – A list of IDs of the layers which were removed.

signal layersWillBeRemoved(layers: List[QgsMapLayer])[source]

Emitted when one or more layers are about to be removed from the store.

Parameters:

layers (List[QgsMapLayer]) – A list of layers which are to be removed.

See also

layersRemoved()

mapLayer(self, id: str | None) QgsMapLayer | None[source]

Retrieve a pointer to a layer by layer id.

Parameters:

id (Optional[str]) – ID of layer to retrieve

Return type:

Optional[QgsMapLayer]

Returns:

matching layer, or None if no matching layer found

See also

mapLayers()

mapLayers(self) Any

Returns a map of all layers by layer ID.

See also

mapLayer()

See also

layers()

Return type:

Any

mapLayersByName(self, name: str | None) List[QgsMapLayer]

Retrieve a list of matching layers by layer name.

Parameters:

name (Optional[str]) – name of layers to match

Return type:

List[QgsMapLayer]

Returns:

list of matching layers

See also

mapLayer()

See also

mapLayers()

removeAllMapLayers(self)[source]

Removes all registered layers. These layers will also be deleted.

Note

Calling this method will cause the removeAll() signal to be emitted.

See also

removeMapLayer()

removeMapLayer(self, id: str | None)[source]

Remove a layer from the store by layer id.

The specified layer will be removed from the store. The layer will also be deleted.

Parameters:

id (Optional[str]) – ID of the layer to remove

See also

takeMapLayer()

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

Remove a layer from the store.

The specified layer will be removed from the store. The layer will also be deleted.

Parameters:

layer (Optional[QgsMapLayer]) – The layer to remove. None values are ignored.

See also

takeMapLayer()

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

Remove a set of layers from the store.

The specified layers will be removed from the store. These layers will also be deleted.

Parameters:

layers (Iterable[QgsMapLayer]) – A list of layers to remove. None values are ignored.

See also

takeMapLayer()

See also

removeMapLayer()

removeMapLayersById(self, layerIds: Iterable[str | None])

Remove a set of layers from the store by layer ID.

The specified layers will be removed from the store. These layers will also be deleted.

Parameters:

layerIds (Iterable[Optional[str]]) – list of IDs of the layers to remove

See also

takeMapLayer()

See also

removeMapLayer()

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

Takes a layer from the store. If the layer was owned by the store, the layer will be returned without deleting it. The caller takes ownership of the layer and is responsible for deleting it.

See also

removeMapLayer()

Parameters:

layer (Optional[QgsMapLayer])

Return type:

Optional[QgsMapLayer]

transferLayersFromStore(self, other: QgsMapLayerStore | None)[source]

Transfers all the map layers contained within another map layer store and adds them to this store. Note that other and this store must have the same thread affinity.

Parameters:

other (Optional[QgsMapLayerStore])

validCount(self) int[source]

Returns the number of valid layers contained in the store.

Added in version 3.6.

Return type:

int

validMapLayers(self) Any

Returns a map of all valid layers by layer ID.

See also

mapLayer()

See also

layers()

Added in version 3.6.

Return type:

Any