Skip to content
Permalink
Browse files

Add a template method to get a casted maplayer

This simplifies the casting of maplayers when accessing them from the layer registry.
  • Loading branch information
m-kuhn committed Jan 9, 2019
1 parent 15ddb27 commit 468f25c5b11d7a2294f96d9828ca99e511b0bc06
Showing with 24 additions and 0 deletions.
  1. +24 −0 src/core/qgsproject.h
@@ -702,6 +702,30 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
*/
QgsMapLayer *mapLayer( const QString &layerId ) const;

#ifndef SIP_RUN

/**
* Retrieve a pointer to a registered layer by \p layerId converted
* to type T. This is a convenience template.
* A nullptr will be returned if the layer is not found or
* if it cannot be cast to type T.
*
* \code{cpp}
* QgsVectorLayer *layer = project->mapLayer<QgsVectorLayer*>( layerId );
* \endcode
*
* \see mapLayer()
* \see mapLayers()
*
* \since QGIS 3.6
*/
template <class T>
T mapLayer( const QString &layerId ) const
{
return qobject_cast<T>( mapLayer( layerId ) );
}
#endif

/**
* Retrieve a list of matching registered layers by layer name.
* \param layerName name of layers to match

0 comments on commit 468f25c

Please sign in to comment.
You can’t perform that action at this time.