5

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?

asked Jun 29, 2013 at 20:02

1 Answer 1

5

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) ] )
answered Jun 30, 2013 at 11:37

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.