Skip to content

Commit d1e2978

Browse files
committed
Add warning to QgsLayerTreeLayer::layer() dox
Because the returned pointer must ALWAYS be tested before being dereferenced
1 parent 6944240 commit d1e2978

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

python/core/auto_generated/layertree/qgslayertreelayer.sip.in

+20
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,32 @@ Constructor for QgsLayerTreeLayer using weak references to layer ID, ``name``, p
3939
%End
4040

4141
QString layerId() const;
42+
%Docstring
43+
Returns the ID for the map layer associated with this node.
44+
45+
.. seealso:: :py:func:`layer`
46+
%End
4247

4348
QgsMapLayer *layer() const;
49+
%Docstring
50+
Returns the map layer associated with this node.
51+
52+
.. warning::
53+
54+
This can be (and often is!) a None, e.g. in the case of a layer node representing a layer
55+
which has not yet been fully loaded into a project, or a layer node representing a layer
56+
with an invalid data source. The returned pointer must ALWAYS be checked to avoid dereferencing a None.
57+
58+
.. seealso:: :py:func:`layerId`
59+
%End
4460

4561
virtual QString name() const;
4662

4763
%Docstring
4864
Returns the layer's name.
4965

66+
.. seealso:: :py:func:`setName`
67+
5068
.. versionadded:: 3.0
5169
%End
5270

@@ -55,6 +73,8 @@ Returns the layer's name.
5573
%Docstring
5674
Sets the layer's name.
5775

76+
.. seealso:: :py:func:`name`
77+
5878
.. versionadded:: 3.0
5979
%End
6080

src/core/layertree/qgslayertreelayer.h

+20
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,38 @@ class CORE_EXPORT QgsLayerTreeLayer : public QgsLayerTreeNode
5454
*/
5555
explicit QgsLayerTreeLayer( const QString &layerId, const QString &name = QString(), const QString &source = QString(), const QString &provider = QString() );
5656

57+
/**
58+
* Returns the ID for the map layer associated with this node.
59+
*
60+
* \see layer()
61+
*/
5762
QString layerId() const { return mRef.layerId; }
5863

64+
/**
65+
* Returns the map layer associated with this node.
66+
*
67+
* \warning This can be (and often is!) a nullptr, e.g. in the case of a layer node representing a layer
68+
* which has not yet been fully loaded into a project, or a layer node representing a layer
69+
* with an invalid data source. The returned pointer must ALWAYS be checked to avoid dereferencing a nullptr.
70+
*
71+
* \see layerId()
72+
*/
5973
QgsMapLayer *layer() const { return mRef.get(); }
6074

6175
/**
6276
* Returns the layer's name.
77+
*
78+
* \see setName()
79+
*
6380
* \since QGIS 3.0
6481
*/
6582
QString name() const override;
6683

6784
/**
6885
* Sets the layer's name.
86+
*
87+
* \see name()
88+
*
6989
* \since QGIS 3.0
7090
*/
7191
void setName( const QString &n ) override;

0 commit comments

Comments
 (0)