Skip to content

Commit f683803

Browse files
committed
QgsMaplayerRegistry function to get only vector or raster layers
1 parent c58a198 commit f683803

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/core/qgsmaplayerregistry.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ void QgsMapLayerRegistry::reloadAllLayers()
191191
}
192192
}
193193

194-
const QMap<QString, QgsMapLayer*>& QgsMapLayerRegistry::mapLayers()
194+
QMap<QString, QgsMapLayer*> QgsMapLayerRegistry::mapLayers()
195195
{
196196
return mMapLayers;
197197
}

src/core/qgsmaplayerregistry.h

+26-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,32 @@ class CORE_EXPORT QgsMapLayerRegistry : public QObject
5050
QList<QgsMapLayer *> mapLayersByName( const QString& layerName );
5151

5252
//! Retrieve the mapLayers collection (mainly intended for use by projection)
53-
const QMap<QString, QgsMapLayer*> & mapLayers();
53+
QMap<QString, QgsMapLayer*> mapLayers();
54+
55+
/**
56+
* Get map layers of a certain type.
57+
*
58+
* Example:
59+
*
60+
* QVector<QgsVectorLayer*> vectorLayers = QgsMapLayerRegistry::instance()->layers<QgsVectorLayer*>();
61+
*
62+
* @note not available in Python bindings
63+
* @note added in QGIS 2.16
64+
*/
65+
template <typename T>
66+
QVector<T> layers() const
67+
{
68+
QVector<T> layers;
69+
Q_FOREACH ( QgsMapLayer* layer, mMapLayers.values() )
70+
{
71+
T tLayer = qobject_cast<T>( layer );
72+
if ( tLayer )
73+
{
74+
layers << tLayer;
75+
}
76+
}
77+
return layers;
78+
}
5479

5580
/**
5681
* @brief

0 commit comments

Comments
 (0)