QgsMapLayerStore class

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

Constructors, destructors, conversion operators

QgsMapLayerStore(QObject* parent = nullptr) explicit
Constructor for QgsMapLayerStore.

Public functions

auto addMapLayer(QgsMapLayer* layer, bool takeOwnership = true) -> QgsMapLayer*
Add a layer to the store.
auto addMapLayers(const QList<QgsMapLayer*>& layers, bool takeOwnership = true) -> QList<QgsMapLayer*>
Add a list of layers to the store.
auto count() const -> int
Returns the number of layers contained in the store.
template<typename T>
auto layers() const -> QVector<T>
Returns a list of registered map layers with a specified layer type.
auto mapLayer(const QString& id) const -> QgsMapLayer*
Retrieve a pointer to a layer by layer id.
auto mapLayers() const -> QMap<QString, QgsMapLayer*>
Returns a map of all layers by layer ID.
auto mapLayersByName(const QString& name) const -> QList<QgsMapLayer*>
Retrieve a list of matching layers by layer name.
void removeAllMapLayers()
Removes all registered layers.
void removeMapLayer(const QString& id)
Remove a layer from the store by layer id.
void removeMapLayer(QgsMapLayer* layer)
Remove a layer from the store.
void removeMapLayers(const QStringList& layerIds)
Remove a set of layers from the store by layer ID.
void removeMapLayers(const QList<QgsMapLayer*>& layers)
Remove a set of layers from the store.
auto takeMapLayer(QgsMapLayer* layer) -> QgsMapLayer*
Takes a layer from the store.
void transferLayersFromStore(QgsMapLayerStore* other)
Transfers all the map layers contained within another map layer store and adds them to this store.
auto validCount() const -> int
Returns the number of valid layers contained in the store.
auto validMapLayers() const -> QMap<QString, QgsMapLayer*>
Returns a map of all valid layers by layer ID.

Signals

void allLayersRemoved()
Emitted when all layers are removed, before layersWillBeRemoved() and layerWillBeRemoved() signals are emitted.
void layerRemoved(const QString& layerId)
Emitted after a layer was removed from the store.
void layersAdded(const QList<QgsMapLayer*>& layers)
Emitted when one or more layers were added to the store.
void layersRemoved(const QStringList& layerIds)
Emitted after one or more layers were removed from the store.
void layersWillBeRemoved(const QStringList& layerIds)
Emitted when one or more layers are about to be removed from the store.
void layersWillBeRemoved(const QList<QgsMapLayer*>& layers)
Emitted when one or more layers are about to be removed from the store.
void layerWasAdded(QgsMapLayer* layer)
Emitted when a layer was added to the store.
void layerWillBeRemoved(const QString& layerId)
Emitted when a layer is about to be removed from the store.
void layerWillBeRemoved(QgsMapLayer* layer)
Emitted when a layer is about to be removed from the store.

Function documentation

QgsMapLayer* QgsMapLayerStore::addMapLayer(QgsMapLayer* layer, bool takeOwnership = true)

Add a layer to the store.

Parameters
layer A layer to add to the store
takeOwnership Ownership will be transferred to the layer store. If you specify false here you have take care of deleting the layers yourself. Not available in Python.
Returns nullptr if unable to add layer, otherwise pointer to newly added layer

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.

QList<QgsMapLayer*> QgsMapLayerStore::addMapLayers(const QList<QgsMapLayer*>& layers, bool takeOwnership = true)

Add a list of layers to the store.

Parameters
layers A list of layer which should be added to the store.
takeOwnership Ownership will be transferred to the layer store. If you specify false here you have take care of deleting the layers yourself. Not available in Python.
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.

Ownership of the layers is transferred to the store.

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

template<typename T>
QVector<T> QgsMapLayerStore::layers() const

Returns a list of registered map layers with a specified layer type.

Example:

QVector<QgsVectorLayer*> vectorLayers = store->layers<QgsVectorLayer*>();

QgsMapLayer* QgsMapLayerStore::mapLayer(const QString& id) const

Retrieve a pointer to a layer by layer id.

Parameters
id ID of layer to retrieve
Returns matching layer, or nullptr if no matching layer found

QMap<QString, QgsMapLayer*> QgsMapLayerStore::mapLayers() const

Returns a map of all layers by layer ID.

QList<QgsMapLayer*> QgsMapLayerStore::mapLayersByName(const QString& name) const

Retrieve a list of matching layers by layer name.

Parameters
name name of layers to match
Returns list of matching layers

void QgsMapLayerStore::removeAllMapLayers()

Removes all registered layers.

These layers will also be deleted.

void QgsMapLayerStore::removeMapLayer(const QString& id)

Remove a layer from the store by layer id.

Parameters
id ID of the layer to remove

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

void QgsMapLayerStore::removeMapLayer(QgsMapLayer* layer)

Remove a layer from the store.

Parameters
layer The layer to remove. Null pointers are ignored.

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

void QgsMapLayerStore::removeMapLayers(const QStringList& layerIds)

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

Parameters
layerIds list of IDs of the layers to remove

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

void QgsMapLayerStore::removeMapLayers(const QList<QgsMapLayer*>& layers)

Remove a set of layers from the store.

Parameters
layers A list of layers to remove. Null pointers are ignored.

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

QgsMapLayer* QgsMapLayerStore::takeMapLayer(QgsMapLayer* layer)

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.

void QgsMapLayerStore::transferLayersFromStore(QgsMapLayerStore* other)

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.

int QgsMapLayerStore::validCount() const

Returns the number of valid layers contained in the store.

QMap<QString, QgsMapLayer*> QgsMapLayerStore::validMapLayers() const

Returns a map of all valid layers by layer ID.

void QgsMapLayerStore::allLayersRemoved() signal

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.

void QgsMapLayerStore::layerRemoved(const QString& layerId) signal

Emitted after a layer was removed from the store.

Parameters
layerId The ID of the layer removed.

void QgsMapLayerStore::layersAdded(const QList<QgsMapLayer*>& layers) signal

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

Parameters
layers List of layers which have been added.

void QgsMapLayerStore::layersRemoved(const QStringList& layerIds) signal

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

Parameters
layerIds A list of IDs of the layers which were removed.

void QgsMapLayerStore::layersWillBeRemoved(const QStringList& layerIds) signal

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

Parameters
layerIds A list of IDs for the layers which are to be removed.

void QgsMapLayerStore::layersWillBeRemoved(const QList<QgsMapLayer*>& layers) signal

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

Parameters
layers A list of layers which are to be removed.

void QgsMapLayerStore::layerWasAdded(QgsMapLayer* layer) signal

Emitted when a layer was added to the store.

void QgsMapLayerStore::layerWillBeRemoved(const QString& layerId) signal

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

Parameters
layerId The ID of the layer to be removed.

void QgsMapLayerStore::layerWillBeRemoved(QgsMapLayer* layer) signal

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

Parameters
layer The layer to be removed.