I am trying to use layer.changeAttributeValue(fid, fieldIndex, value) to add values to a field in QGIS

Basically, I'm creating a plugin which will, if a checkbox is ticked, create a new field, then iterate through the dataset, filling that field with the x (easting) coordinate of each point (I also have another checkbox for the same for the y coordinate). As such, I cannot set the field index beforehand because I don't know which will be ticked.

So - how can I get the field index of the new layer in my script using Python?

Also (sorry, I'm new to this), how can I get the fid of the current feature as I iterate through the dataset?

1 Answer 1

int QgsVectorDataProvider::fieldNameIndex (const QString & fieldName) const

Returns the index of a field name or -1 if the field does not exist.



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.