2

I'm new to QGIS python development. I'm trying to create new features in an existing vector layer and then to display the Feature Form so that the user can fill the attribute values.

The problem is, after the code is executed (after I fill the features atribute values and click Ok), an error is shown: Feature 0 for attribute update not found.

After that, I run again the code to create another feature with different attribute values. No error appears, but I notice that the attribute values from the firts created feature are exact like the attribute values from the second created feature.

Here is the code:

mylayer = QgsProject.instance().mapLayersByName('my layer name')[0]
mylayer.startEditing()
feat = QgsFeature ()
feat.setFields(mylayer.fields())

//here is the code that creates the geometry//

feat.setGeometry(geom)
self.iface.openFeatureForm(mylayer, feat, False)
mylayer.addFeature(feat)
mylayer.commitChanges()

What am I doing wrong? I don't know if it helps, but mylayer is an empty layer.

1

You have to add the new feature to the layer before you open the FeatureForm. So just put the line

mylayer.addFeature(feat)

before

self.iface.openFeatureForm(mylayer, feat, False)

and you should be fine.

1
  • Thank you for your reply. It solved my problem!
    – zlatna
    Jan 6 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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