3

Workflow: Start edit mode -> New feature is created -> On the console (pyqgis), new attributes are set -> Exit edit mode.

Problem: The attributes are not saved. Only after completion of the editing, I can edit the attributes.

The feature is created and during the editing, I try the following:

newFeature = layer.selectedFeatures()[0] layer.dataProvider().changeAttributeValues({ newFeature.id() : {0 : 'foo'} }) layer.updateFields()

The Layer already has an attribute table and some features. After editing, the above code works. During editing, no attributes can be set.

Before the feature appears in the map, it opens a window. In this window, attributes can be entered. This also works. But I would like to change the attributes later, after the feature has been displayed in qgis.

  • I have rolled back the new question you placed within this one. If you have a new question then please ask it separately. Also, please include any text like code, errors, etc as text rather than pictures so that it is available to future searches by people with the same issue. I note that you have not yet taken the Tour that introduces new users to the site protocols. – PolyGeo Apr 8 '16 at 22:54
5

I think if you want to change attributes using the dataProvider() method, it must retrieve the written table before you can edit it (i.e. the attributes of the new feature must be saved beforehand). Instead, you can just directly change the attributes without having to access the dataProvider():

layer = qgis.utils.iface.activeLayer()
for f in layer.selectedFeatures():
    layer.changeAttributeValue(f.id(), 0, 'foo')

This allows you to edit the attributes of any selected features before saving any changes.

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.