1

I have created a custom form (for mapping bird species in the field with different attributes like species, type of observation, number of birds, date, ...) with some Python logic.

One important feature of the Python logic is to provide a custom drop-down list with the possible species. A list of the most recently used species and the most often used species are added before adding the complete list of species. This way I want to minimize the amount of scrolling needed, because scrolling on a small tablet PC in the field is quite time consuming.

The form opens fine and shows the existing values (in the case of editing an existing feature). I can work with all fields as I want to, including the custom dropdown lists.

But when I finish the form by clicking the "OK" button, the changed values (in the case of editing an existing feature) or the new values (in the case of a new feature) are not written back to the attribute table of the shapefile.

So here is my question:

How can I force QGIS to write back all edited values to the attribute table??

I tried different commands like:

currentFeature.setAttributes() 
currentFeature.setAttribute() 
currentLayer.updateFeature(currentFeature)

but nothing has worked and I find contradictory / confusing information about these commands in the net. I guess these commands were modified in the last versions of QGIS.

System: Windows 7 64

QGIS Version: 2.8 (tried before with older versions with same result)

Python Version: 2.7

nmtoken
13.6k5 gold badges39 silver badges91 bronze badges
asked Mar 8, 2015 at 17:57

1 Answer 1

1

Next code:

def fillFields(list, idx):
 i =0
 for item in list:
 new_values = {idx:item }
 provider.changeAttributeValues({i: new_values})
 i += 1
from PyQt4.QtCore import *
layer = iface.activeLayer()
provider = layer.dataProvider()
field = QgsField('test', QVariant.Double) #create field
provider.addAttributes([field]) #add field to attibute table
layer.updateFields() #update fields
n= layer.featureCount() #count features
field1 = range(n) #create list with n values
fillFields(field1, 2) #introduce list 'field1' in field with index 2 (name test)

it creates in the vector layer used as Active Layer a field named 'test' (double) and subsequently, the function 'fillFields' uses as parameters one arbitrary list of integers and the index field name of 'test' to update (modify) their values from NULL (default when the field is created).

Before running the script:

enter image description here

After running the script:

enter image description here

answered Mar 8, 2015 at 19:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.