@@ -229,8 +229,18 @@ class CORE_EXPORT QgsFeature
229
229
230
230
/* *
231
231
* Returns the feature's attributes.
232
- * \see setAttributes
233
- * \note Alternatively in Python: iterate feature, eg. @code [attr for attr in feature] @endcode
232
+ *
233
+ * Alternatively, in Python it is possible to directly iterate over a feature in order to retrieve
234
+ * its attributes:
235
+ *
236
+ * \code{.py}
237
+ * feature = QgsFeature()
238
+ * feature.setAttributes([11, 'string value', 55.5])
239
+ * for attr in feature:
240
+ * print(attr)
241
+ * \endcode
242
+ *
243
+ * \see setAttributes()
234
244
* \since QGIS 2.9
235
245
*/
236
246
QgsAttributes attributes () const ;
@@ -279,10 +289,23 @@ class CORE_EXPORT QgsFeature
279
289
*
280
290
* If the attribute was successfully set then the feature will be automatically marked as valid (see isValid()).
281
291
*
292
+ * Alternatively, in Python it is possible to directly set a field's value via the field's index:
293
+ *
294
+ * \code{.py}
295
+ * fields = QgsFields()
296
+ * fields.append(QgsField('my_id', QVariant.Int))
297
+ * fields.append(QgsField('name', QVariant.String))
298
+ * feature = QgsFeature(fields)
299
+ *
300
+ * # set the "name" field value
301
+ * feature[1] = "my name"
302
+ * # set the "my_id" field value
303
+ * feature[0] = 55
304
+ * \endcode
305
+ *
282
306
* \param field the index of the field to set
283
307
* \param attr the value of the attribute
284
308
* \throws KeyError if the field index does not exist
285
- * \note Alternatively in Python: @code feature[field] = attr @endcode
286
309
* \see setAttributes()
287
310
*/
288
311
bool setAttribute ( int field, const QVariant &attr / GetWrapper / );
@@ -345,7 +368,7 @@ class CORE_EXPORT QgsFeature
345
368
#ifndef SIP_RUN
346
369
347
370
/* *
348
- * Deletes an attribute and its value .
371
+ * Clear's an attribute's value by its index .
349
372
*
350
373
* \param field the index of the field
351
374
*
@@ -355,12 +378,27 @@ class CORE_EXPORT QgsFeature
355
378
#else
356
379
357
380
/* *
358
- * Deletes an attribute and its value .
381
+ * Clear's an attribute's value by its index .
359
382
*
360
383
* \param field the index of the field
361
384
*
385
+ * Alternatively, in Python it is possible to directly `del` an attribute via its index:
386
+ *
387
+ * \code{.py}
388
+ * feature = QgsFeature()
389
+ * feature.setAttributes([11, 'my feature', 55.5])
390
+ *
391
+ * # will print [11, 'my feature', 55.5]
392
+ * print(feature.attributes())
393
+ *
394
+ * # clear the second attribute
395
+ * del feature[1]
396
+ *
397
+ * # will now print [11, NONE]
398
+ * print(feature.attributes())
399
+ * \endcode
400
+ *
362
401
* \throws KeyError if the field is not found
363
- * \note Alternatively in Python: @code del feature[field] @endcode
364
402
* \see setAttribute()
365
403
*/
366
404
void deleteAttribute ( int field );
@@ -510,10 +548,23 @@ class CORE_EXPORT QgsFeature
510
548
*
511
549
* Calling this method will automatically set the feature as valid (see isValid()).
512
550
*
551
+ * Alternatively, in Python it is possible to directly set a field's value via the field's name:
552
+ *
553
+ * \code{.py}
554
+ * fields = QgsFields()
555
+ * fields.append(QgsField('my_id', QVariant.Int))
556
+ * fields.append(QgsField('name', QVariant.String))
557
+ * feature = QgsFeature(fields)
558
+ *
559
+ * # set the "name" field value
560
+ * feature['name'] = "my name"
561
+ * # set the "my_id" field value
562
+ * feature['my_id'] = 55
563
+ * \endcode
564
+ *
513
565
* \param name The name of the field to set
514
566
* \param value The value to set
515
567
* \throws KeyError if the attribute name could not be converted to an index
516
- * \note Alternatively in Python: @code feature[name] = attr @endcode
517
568
* \see setFields()
518
569
*/
519
570
void setAttribute ( const QString &name, const QVariant &value / GetWrapper / );
@@ -541,25 +592,44 @@ class CORE_EXPORT QgsFeature
541
592
#ifndef SIP_RUN
542
593
543
594
/* *
544
- * Removes an attribute value by field \a name.
595
+ * Clear's an attribute's value by its field \a name.
545
596
*
546
- * Field map must be associated using setFields()before this method can be used.
597
+ * Field map must be associated using setFields() before this method can be used.
547
598
*
548
- * \param name The name of the field to delete
599
+ * \param name The name of the field to clear
549
600
* \returns FALSE if attribute name could not be converted to index
550
601
* \see setFields()
551
602
*/
552
603
bool deleteAttribute ( const QString &name );
553
604
#else
554
605
555
606
/* *
556
- * Removes an attribute value by field \a name.
607
+ * Clear's an attribute's value by its field \a name.
557
608
*
558
- * Field map must be associated using setFields()before this method can be used.
609
+ * Field map must be associated using setFields() before this method can be used.
610
+ *
611
+ * Alternatively, in Python it is possible to directly `del` an attribute via its name:
612
+ *
613
+ * \code{.py}
614
+ * fields = QgsFields()
615
+ * fields.append(QgsField('my_id', QVariant.Int))
616
+ * fields.append(QgsField('name', QVariant.String))
617
+ *
618
+ * feature = QgsFeature(fields)
619
+ * feature.setAttributes([11, 'my feature'])
620
+ *
621
+ * # will print [11, 'my feature']
622
+ * print(feature.attributes())
559
623
*
560
- * \param name The name of the field to delete
624
+ * # clear the 'name' attribute
625
+ * del feature['name']
626
+ *
627
+ * # will now print [11, NULL]
628
+ * print(feature.attributes())
629
+ * \endcode
630
+ *
631
+ * \param name The name of the field to clear
561
632
* \throws KeyError if attribute name could not be converted to index
562
- * \note Alternatively in Python: @code del feature[name] @endcode
563
633
* \see setFields()
564
634
*/
565
635
bool deleteAttribute ( const QString &name );
@@ -598,10 +668,24 @@ class CORE_EXPORT QgsFeature
598
668
*
599
669
* Field map must be associated using setFields() before this method can be used.
600
670
*
671
+ * Alternatively, in Python it is possible to directly retrieve a field's value via the field's name:
672
+ *
673
+ * \code{.py}
674
+ * fields = QgsFields()
675
+ * fields.append(QgsField('my_id', QVariant.Int))
676
+ * fields.append(QgsField('name', QVariant.String))
677
+ * feature = QgsFeature(fields)
678
+ * feature.setAttributes([11, 'my feature'])
679
+ *
680
+ * # print the "name" field value
681
+ * print(feature['name'])
682
+ * # print the "my_id" field value
683
+ * print(feature['my_id'])
684
+ * \endcode
685
+ *
601
686
* \param name The name of the attribute to get
602
687
* \returns The value of the attribute
603
688
* \throws KeyError if the field is not found
604
- * \note Alternatively in Python: @code feature[name] @endcode
605
689
* \see setFields
606
690
*/
607
691
SIP_PYOBJECT attribute ( const QString &name ) const ;
@@ -625,8 +709,6 @@ class CORE_EXPORT QgsFeature
625
709
/* *
626
710
* Lookup attribute value from its index.
627
711
*
628
- * Field map must be associated using setFields() before this method can be used.
629
- *
630
712
* \param fieldIdx The index of the attribute to get
631
713
* \returns The value of the attribute, or an invalid/null variant if no such name exists
632
714
* \see setFields()
@@ -637,12 +719,25 @@ class CORE_EXPORT QgsFeature
637
719
/* *
638
720
* Lookup attribute value from its index.
639
721
*
640
- * Field map must be associated using setFields() before this method can be used.
722
+ * Alternatively, in Python it is possible to directly retrieve a field's value via its index:
723
+ *
724
+ * \code{.py}
725
+ * feature = QgsFeature()
726
+ * feature.setAttributes([11, 'my feature', 55.5])
727
+ *
728
+ * # will print 11
729
+ * print(feature[0])
730
+ *
731
+ * # will print 'my feature'
732
+ * print(feature[1])
733
+ *
734
+ * # will print 55.5
735
+ * print(feature[2])
736
+ * \endcode
641
737
*
642
738
* \param fieldIdx The index of the attribute to get
643
739
* \returns The value of the attribute
644
740
* \throws KeyError if the field is not found
645
- * \note Alternatively in Python: @code feature[fieldIdx] @endcode
646
741
* \see setFields()
647
742
*/
648
743
SIP_PYOBJECT attribute ( int fieldIdx ) const ;
0 commit comments