Skip to main content
1 of 2
Michael Stimson
  • 25.7k
  • 2
  • 35
  • 75

Yes, but you have to know the index of the field:

QgsFeature::setAttribute(int field,const QVariant & attr )  

You can get the fields using QgsFeature::fields and then iterate through them until you find the one you want or QgsFeature::attribute(const QString & name ) to find the field index by name.

The reason for QVariant is that the setAttribute can take Integer, Float, Date and Text types. QgsFeature.attribute(name) returns a variant also of type int if the attribute is found and something else if it can't be found.. be aware of this in your code. A try..except block would be warranted, try to convert to int and except if type is not int.

Michael Stimson
  • 25.7k
  • 2
  • 35
  • 75