QGIS API Documentation 4.3.0-Master (767b36bf018)
Loading...
Searching...
No Matches
QgsMapLayerModel Class Reference

A model for display of map layers in widgets. More...

#include <qgsmaplayermodel.h>

Inheritance diagram for QgsMapLayerModel:

Public Types

enum class  CustomRole : int { LayerId = Qt::UserRole + 1 , Layer , Empty , Additional }
 Custom model roles. More...

Public Member Functions

Q_DECL_DEPRECATED QgsMapLayerModel (const QList< QgsMapLayer * > &layers, QObject *parent=nullptr, QgsProject *project=nullptr)
 QgsMapLayerModel creates a model to display a specific list of layers in a widget.
 QgsMapLayerModel (QgsProject *project, const QList< QgsMapLayer * > &layers, QObject *parent=nullptr)
 QgsMapLayerModel creates a model to display a specific list of layers in a widget.
 QgsMapLayerModel (QgsProject *project, QObject *parent=nullptr)
 QgsMapLayerModel creates a model to display layers in widgets.
 QgsMapLayerModel (QObject *parent=nullptr, QgsProject *project=nullptr)
 QgsMapLayerModel creates a model to display layers in widgets.
QStringList additionalItems () const
 Returns the list of additional (non map layer) items included at the end of the model.
QList< QgsMapLayer * > additionalLayers () const
 Returns the list of additional layers added to the model.
bool allowEmptyLayer () const
 Returns true if the model allows the empty layer ("not set") choice.
bool canDropMimeData (const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const override
void checkAll (Qt::CheckState checkState)
 checkAll changes the checkstate for all the layers
int columnCount (const QModelIndex &parent=QModelIndex()) const override
QVariant data (const QModelIndex &index, int role=Qt::DisplayRole) const override
bool dropMimeData (const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
Qt::ItemFlags flags (const QModelIndex &index) const override
QModelIndex index (int row, int column, const QModelIndex &parent=QModelIndex()) const override
QModelIndex indexFromLayer (QgsMapLayer *layer) const
 indexFromLayer returns the model index for a given layer
bool insertRows (int row, int count, const QModelIndex &parent=QModelIndex()) override
bool itemsCanBeReordered () const
 Returns true if items in the model can be reordered via drag and drop.
bool itemsCheckable () const
 Returns whether the items can be checked or not.
QgsMapLayerlayerFromIndex (const QModelIndex &index) const
 Returns the map layer corresponding to the specified index.
QList< QgsMapLayer * > layersChecked (Qt::CheckState checkState=Qt::Checked)
 Returns the list of layers which are checked (or unchecked).
QMimeData * mimeData (const QModelIndexList &indexes) const override
QStringList mimeTypes () const override
QModelIndex parent (const QModelIndex &child) const override
bool removeRows (int row, int count, const QModelIndex &parent=QModelIndex()) override
QHash< int, QByteArray > roleNames () const override
 Returns strings for all roles supported by this model.
int rowCount (const QModelIndex &parent=QModelIndex()) const override
void setAdditionalItems (const QStringList &items)
 Sets a list of additional (non map layer) items to include at the end of the model.
void setAdditionalLayers (const QList< QgsMapLayer * > &layers)
 Sets a list of additional layers to include in the model.
void setAllowEmptyLayer (bool allowEmpty, const QString &text=QString(), const QIcon &icon=QIcon())
 Sets whether an optional empty layer ("not set") option is present in the model.
bool setData (const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
void setItemsCanBeReordered (bool allow)
 Sets whether items in the model can be reordered via drag and drop.
void setItemsCheckable (bool checkable)
 Defines if layers should be selectable in the widget.
void setLayersChecked (const QList< QgsMapLayer * > &layers)
 Sets which layers are checked in the model.
void setProject (QgsProject *project)
 Sets the QgsProject from which map layers are shown.
void setShowCrs (bool showCrs)
 Sets whether the CRS of layers is also included in the model's display role.
bool showCrs () const
 Returns true if the model includes layer's CRS in the display role.
Qt::DropActions supportedDropActions () const override

Static Public Member Functions

static QIcon iconForLayer (QgsMapLayer *layer)
 Returns the icon corresponding to a specified map layer.

Protected Slots

void addLayers (const QList< QgsMapLayer * > &layers)
void removeLayers (const QStringList &layerIds)

Protected Attributes

QList< QPointer< QgsMapLayer > > mAdditionalLayers
bool mCanReorder = false
bool mItemCheckable = false
QList< QgsMapLayer * > mLayers
QMap< QString, Qt::CheckState > mLayersChecked
QgsProjectmProject = nullptr

Properties

QStringList additionalItems
bool allowEmptyLayer
bool itemsCheckable
bool showCrs

Detailed Description

A model for display of map layers in widgets.

See also
QgsMapLayerProxyModel to sort and/filter the layers
QgsFieldModel to combine in with a field selector.

Definition at line 36 of file qgsmaplayermodel.h.

Member Enumeration Documentation

◆ CustomRole

enum class QgsMapLayerModel::CustomRole : int
strong

Custom model roles.

Note
Prior to QGIS 3.36 this was available as QgsMapLayerModel::ItemDataRole
Since
QGIS 3.36
Enumerator
LayerId 

Stores the map layer ID.

Layer 

Stores pointer to the map layer itself.

Empty 

True if index corresponds to the empty (not set) value.

Additional 

True if index corresponds to an additional (non map layer) item.

Definition at line 54 of file qgsmaplayermodel.h.

Constructor & Destructor Documentation

◆ QgsMapLayerModel() [1/4]

QgsMapLayerModel::QgsMapLayerModel ( QObject * parent = nullptr,
QgsProject * project = nullptr )
explicit

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.

Note
Will be removed in QGIS 5.0. Use the constructor with the explicit QgsProject argument instead.

Definition at line 38 of file qgsmaplayermodel.cpp.

◆ QgsMapLayerModel() [2/4]

QgsMapLayerModel::QgsMapLayerModel ( const QList< QgsMapLayer * > & layers,
QObject * parent = nullptr,
QgsProject * project = nullptr )
explicit

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.

Deprecated
QGIS 4.4. Will be removed in QGIS 5.0. Use the constructor with the explicit QgsProject argument instead.

Definition at line 33 of file qgsmaplayermodel.cpp.

◆ QgsMapLayerModel() [3/4]

QgsMapLayerModel::QgsMapLayerModel ( QgsProject * project,
QObject * parent = nullptr )
explicit

QgsMapLayerModel creates a model to display layers in widgets.

Layers are taken from project.

Since
QGIS 4.4

Definition at line 53 of file qgsmaplayermodel.cpp.

◆ QgsMapLayerModel() [4/4]

QgsMapLayerModel::QgsMapLayerModel ( QgsProject * project,
const QList< QgsMapLayer * > & layers,
QObject * parent = nullptr )
explicit

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

Since
QGIS 4.4

Definition at line 42 of file qgsmaplayermodel.cpp.

Member Function Documentation

◆ additionalItems()

QStringList QgsMapLayerModel::additionalItems ( ) const
inline

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

See also
setAdditionalItems()

Definition at line 202 of file qgsmaplayermodel.h.

◆ additionalLayers()

QList< QgsMapLayer * > QgsMapLayerModel::additionalLayers ( ) const

Returns the list of additional layers added to the model.

See also
setAdditionalLayers()
Since
QGIS 3.22

Definition at line 236 of file qgsmaplayermodel.cpp.

◆ addLayers

void QgsMapLayerModel::addLayers ( const QList< QgsMapLayer * > & layers)
protectedslot

Definition at line 262 of file qgsmaplayermodel.cpp.

◆ allowEmptyLayer()

bool QgsMapLayerModel::allowEmptyLayer ( ) const
inline

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

See also
setAllowEmptyLayer()

Definition at line 151 of file qgsmaplayermodel.h.

◆ canDropMimeData()

bool QgsMapLayerModel::canDropMimeData ( const QMimeData * data,
Qt::DropAction action,
int row,
int column,
const QModelIndex & parent ) const
override

Definition at line 510 of file qgsmaplayermodel.cpp.

◆ checkAll()

void QgsMapLayerModel::checkAll ( Qt::CheckState checkState)

checkAll changes the checkstate for all the layers

Definition at line 104 of file qgsmaplayermodel.cpp.

◆ columnCount()

int QgsMapLayerModel::columnCount ( const QModelIndex & parent = QModelIndex()) const
override

Definition at line 314 of file qgsmaplayermodel.cpp.

◆ data()

QVariant QgsMapLayerModel::data ( const QModelIndex & index,
int role = Qt::DisplayRole ) const
override

Definition at line 321 of file qgsmaplayermodel.cpp.

◆ dropMimeData()

bool QgsMapLayerModel::dropMimeData ( const QMimeData * data,
Qt::DropAction action,
int row,
int column,
const QModelIndex & parent )
override

Definition at line 541 of file qgsmaplayermodel.cpp.

◆ flags()

Qt::ItemFlags QgsMapLayerModel::flags ( const QModelIndex & index) const
override

Definition at line 432 of file qgsmaplayermodel.cpp.

◆ iconForLayer()

QIcon QgsMapLayerModel::iconForLayer ( QgsMapLayer * layer)
static

Returns the icon corresponding to a specified map layer.

Definition at line 580 of file qgsmaplayermodel.cpp.

◆ index()

QModelIndex QgsMapLayerModel::index ( int row,
int column,
const QModelIndex & parent = QModelIndex() ) const
override

Definition at line 281 of file qgsmaplayermodel.cpp.

◆ indexFromLayer()

QModelIndex QgsMapLayerModel::indexFromLayer ( QgsMapLayer * layer) const

indexFromLayer returns the model index for a given layer

See also
layerFromIndex()

Definition at line 172 of file qgsmaplayermodel.cpp.

◆ insertRows()

bool QgsMapLayerModel::insertRows ( int row,
int count,
const QModelIndex & parent = QModelIndex() )
override

Definition at line 459 of file qgsmaplayermodel.cpp.

◆ itemsCanBeReordered()

bool QgsMapLayerModel::itemsCanBeReordered ( ) const

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

See also
setItemsCanBeReordered()
Since
QGIS 3.14

Definition at line 99 of file qgsmaplayermodel.cpp.

◆ itemsCheckable()

bool QgsMapLayerModel::itemsCheckable ( ) const
inline

Returns whether the items can be checked or not.

Definition at line 176 of file qgsmaplayermodel.h.

◆ layerFromIndex()

QgsMapLayer * QgsMapLayerModel::layerFromIndex ( const QModelIndex & index) const

Returns the map layer corresponding to the specified index.

See also
indexFromLayer()

Definition at line 180 of file qgsmaplayermodel.cpp.

◆ layersChecked()

QList< QgsMapLayer * > QgsMapLayerModel::layersChecked ( Qt::CheckState checkState = Qt::Checked)

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

Definition at line 144 of file qgsmaplayermodel.cpp.

◆ mimeData()

QMimeData * QgsMapLayerModel::mimeData ( const QModelIndexList & indexes) const
override

Definition at line 517 of file qgsmaplayermodel.cpp.

◆ mimeTypes()

QStringList QgsMapLayerModel::mimeTypes ( ) const
override

Definition at line 503 of file qgsmaplayermodel.cpp.

◆ parent()

QModelIndex QgsMapLayerModel::parent ( const QModelIndex & child) const
override

Definition at line 299 of file qgsmaplayermodel.cpp.

◆ removeLayers

void QgsMapLayerModel::removeLayers ( const QStringList & layerIds)
protectedslot

Definition at line 241 of file qgsmaplayermodel.cpp.

◆ removeRows()

bool QgsMapLayerModel::removeRows ( int row,
int count,
const QModelIndex & parent = QModelIndex() )
override

Definition at line 476 of file qgsmaplayermodel.cpp.

◆ roleNames()

QHash< int, QByteArray > QgsMapLayerModel::roleNames ( ) const
override

Returns strings for all roles supported by this model.

Note
Available only with Qt5 (Python and c++)

Definition at line 423 of file qgsmaplayermodel.cpp.

◆ rowCount()

int QgsMapLayerModel::rowCount ( const QModelIndex & parent = QModelIndex()) const
override

Definition at line 306 of file qgsmaplayermodel.cpp.

◆ setAdditionalItems()

void QgsMapLayerModel::setAdditionalItems ( const QStringList & items)

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
additionalItems()

Definition at line 185 of file qgsmaplayermodel.cpp.

◆ setAdditionalLayers()

void QgsMapLayerModel::setAdditionalLayers ( const QList< QgsMapLayer * > & layers)

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
additionalLayers()
Since
QGIS 3.22

Definition at line 210 of file qgsmaplayermodel.cpp.

◆ setAllowEmptyLayer()

void QgsMapLayerModel::setAllowEmptyLayer ( bool allowEmpty,
const QString & text = QString(),
const QIcon & icon = 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.

See also
allowEmptyLayer()

Definition at line 114 of file qgsmaplayermodel.cpp.

◆ setData()

bool QgsMapLayerModel::setData ( const QModelIndex & index,
const QVariant & value,
int role = Qt::EditRole )
override

Definition at line 585 of file qgsmaplayermodel.cpp.

◆ setItemsCanBeReordered()

void QgsMapLayerModel::setItemsCanBeReordered ( bool allow)

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

See also
itemsCanBeReordered()
Since
QGIS 3.14

Definition at line 94 of file qgsmaplayermodel.cpp.

◆ setItemsCheckable()

void QgsMapLayerModel::setItemsCheckable ( bool checkable)

Defines if layers should be selectable in the widget.

Definition at line 89 of file qgsmaplayermodel.cpp.

◆ setLayersChecked()

void QgsMapLayerModel::setLayersChecked ( const QList< QgsMapLayer * > & layers)

Sets which layers are checked in the model.

Definition at line 158 of file qgsmaplayermodel.cpp.

◆ setProject()

void QgsMapLayerModel::setProject ( QgsProject * project)

Sets the QgsProject from which map layers are shown.

Since
QGIS 3.24

Definition at line 65 of file qgsmaplayermodel.cpp.

◆ setShowCrs()

void QgsMapLayerModel::setShowCrs ( bool showCrs)

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

See also
showCrs()

Definition at line 135 of file qgsmaplayermodel.cpp.

◆ showCrs()

bool QgsMapLayerModel::showCrs ( ) const
inline

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

See also
setShowCrs()

Definition at line 163 of file qgsmaplayermodel.h.

◆ supportedDropActions()

Qt::DropActions QgsMapLayerModel::supportedDropActions ( ) const
override

Definition at line 575 of file qgsmaplayermodel.cpp.

Member Data Documentation

◆ mAdditionalLayers

QList< QPointer<QgsMapLayer> > QgsMapLayerModel::mAdditionalLayers
protected

Definition at line 257 of file qgsmaplayermodel.h.

◆ mCanReorder

bool QgsMapLayerModel::mCanReorder = false
protected

Definition at line 260 of file qgsmaplayermodel.h.

◆ mItemCheckable

bool QgsMapLayerModel::mItemCheckable = false
protected

Definition at line 259 of file qgsmaplayermodel.h.

◆ mLayers

QList<QgsMapLayer *> QgsMapLayerModel::mLayers
protected

Definition at line 256 of file qgsmaplayermodel.h.

◆ mLayersChecked

QMap<QString, Qt::CheckState> QgsMapLayerModel::mLayersChecked
protected

Definition at line 258 of file qgsmaplayermodel.h.

◆ mProject

QgsProject* QgsMapLayerModel::mProject = nullptr
protected

Definition at line 262 of file qgsmaplayermodel.h.

Property Documentation

◆ additionalItems

QStringList QgsMapLayerModel::additionalItems
readwrite

Definition at line 43 of file qgsmaplayermodel.h.

◆ allowEmptyLayer

bool QgsMapLayerModel::allowEmptyLayer
readwrite

Definition at line 40 of file qgsmaplayermodel.h.

◆ itemsCheckable

bool QgsMapLayerModel::itemsCheckable
readwrite

Definition at line 42 of file qgsmaplayermodel.h.

◆ showCrs

bool QgsMapLayerModel::showCrs
readwrite

Definition at line 41 of file qgsmaplayermodel.h.


The documentation for this class was generated from the following files: