[GIS] Add attributes to vector layer with pyqgis (QGIS 2.0 API)

pyqgis

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).
http://hub.qgis.org/wiki/quantum-gis/Python_plugin_API_changes_from_18_to_20

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)
try:
    if ind == -1:
      if caps & QgsVectorDataProvider.AddAttributes:
        res = provider.addAttributes( [ QgsField(name,double) ] )
except:
    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?

Best Answer

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) ] )

to

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