Skip to content

Commit dc860f7

Browse files
committedJun 5, 2013
When calling setFields, automatically initalize attributes
That what you want most of the time when creating a new feature within a plugin. Therefore defaults to true when used from python, but to false when used from C++
1 parent e21f160 commit dc860f7

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed
 

‎python/core/qgsfeature.sip

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,17 @@ class QgsFeature
158158
*/
159159
void setGeometryAndOwnership( unsigned char * geom /Transfer/, size_t length );
160160

161-
/** Assign a field map with the feature to allow attribute access by attribute name
161+
/** Assign a field map with the feature to allow attribute access by
162+
* attribute name
163+
*
164+
* @param fields The attribute fields which this feature holds
165+
* @param initAttributes If true, attributes are initialized. Clears any
166+
* data previously assigned.
167+
* C++: Defaults to false
168+
* Python: Defaults to true
162169
* @note added in 2.0
163170
*/
164-
void setFields( const QgsFields* fields );
171+
void setFields( const QgsFields* fields, bool initAttributes = true );
165172

166173
/** Get associated field map. may be NULL
167174
* @note added in 2.0

‎src/core/qgsfeature.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,15 @@ void QgsFeature::setGeometryAndOwnership( unsigned char *geom, size_t length )
159159
setGeometry( g );
160160
}
161161

162+
void QgsFeature::setFields( const QgsFields* fields, bool initAttributes )
163+
{
164+
mFields = fields;
165+
if ( initAttributes )
166+
{
167+
this->initAttributes( fields->count() );
168+
}
169+
}
170+
162171

163172
bool QgsFeature::isValid() const
164173
{

‎src/core/qgsfeature.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,18 @@ class CORE_EXPORT QgsFeature
189189
void setGeometryAndOwnership( unsigned char * geom, size_t length );
190190

191191
/** Assign a field map with the feature to allow attribute access by attribute name
192+
*
193+
* @param fields The attribute fields which this feature holds. When used from python, make sure
194+
* a copy of the fields is held by python, as ownership stays there.
195+
* I.e. Do not call feature.setFields( myDataProvider.fields() ) but instead call
196+
* myFields = myDataProvider.fields()
197+
* feature.setFields( myFields )
198+
* @param initAttributes If true, attributes are initialized. Clears any data previously assigned.
199+
* C++: Defaults to false
200+
* Python: Defaults to true
192201
* @note added in 2.0
193202
*/
194-
void setFields( const QgsFields* fields ) { mFields = fields; }
203+
void setFields( const QgsFields* fields, bool initAttributes = false );
195204

196205
/** Get associated field map. may be NULL
197206
* @note added in 2.0

0 commit comments

Comments
 (0)
Please sign in to comment.