Part of my QGIS plugin is responsible for appending a new column to a vector layer and fill this column with values calculated earlier.
I'm able to append the column using
layerPoly = QgsMapLayerRegistry.instance().mapLayersByName(PolyName) layerPoly.dataProvider().addAttributes([QgsField(newName, QVariant.Double, "double", 10, 2)]) layerPoly.updateFields()
So far it works, creating a new field of a
double type, length
10 and precision
Now starts the part of updating attributes of each feature:
columnid = layerPoly.fieldNameIndex(PolyName) layerPoly.startEditing() for feature in range(len(self.polygons)): # self.polygons is a list of features from layerPoly feat = self.polygons[feature] fid = feat.id() # self.y is a list of values that has length same as self.polygons item = round(self.y[feature],2) layerPoly.changeAttributeValue(fid, columnid, item) layerPoly.commitChanges()
print type(fid)returns type:
QgsFeatureIdas I think it should?
print type(columnid)returns type:
print type(item)returns type:
What causes the
changeAttributeValue to be
False? I thought it's
fid's type not
int, but compared to other codes I have seen on the Internet, this
feat.id() part is quite the same, so it shouldn't be an issue.
I'm using QGIS 2.8 and PyQt4==4.11.4.