Skip to content

Commit

Permalink
Add a template method to get a casted maplayer
Browse files Browse the repository at this point in the history
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 468f25c
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/core/qgsproject.h
Expand Up @@ -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
Expand Down

0 comments on commit 468f25c

Please sign in to comment.