i am trying to make a QGIS plugin ready for the new QGIS API in the current development branch (for the upcoming QGIS 2.0 release).

However i struggle a bit on how to add new attributes to the attribute table.
Up to now i have added new attributes like this:

# Working in QGIS Lisboa
layer = ... # QGsVectorLayer object
name = "newAttribute" 
provider = layer.dataProvider()
caps = provider.capabilities()
# Check if attribute is already there, return "-1" if not
ind = provider.fieldNameIndex(name)
    if ind == -1:
      if caps & QgsVectorDataProvider.AddAttributes:
        res = provider.addAttributes( [ QgsField(name,double) ] )
    return False

In the recent QGIS dev. version these steps aren't working anymore (the attribute column isn't added) and i was not able to find the solution in the above mentioned wiki-page.
Can someone give me hint?


Okay, i found the solution after trying multiple random things. Contrary to what the wiki-page states you definitely have to set the type as QVariant.double type for the QGsField.
So i just had to change this line here:

res = provider.addAttributes( [ QgsField(name,double) ] )


res = provider.addAttributes( [ QgsField(name,QVariant.Double) ] )

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.