Bug report #2378

addAttribute() doesn't work

Added by luca76 - about 12 years ago. Updated about 12 years ago.

Category:Python plugins
Affected QGIS version: Regression?:No
Operating System:Debian Easy fix?:No
Pull Request or Patch supplied: Resolution:worksforme
Crashes QGIS or corrupts data: Copied to github as #:12438


Hi, I wrote a python plugin that does a:

feat = [[QgsFeature]]()
x = 0
y = "hello" 
feat.addAttribute (x, QString(y))

In Windows it works well, but in Ubuntu Jaunty (and QGIS 1.4.0) it gives this error:

[[TypeError]]: argument 2 of [[QgsFeature]].addAttribute() has an invalid type


#1 Updated by luca76 - about 12 years ago

note: I tried to remove QString(), same result.

#2 Updated by luca76 - about 12 years ago

err... I mispelled the title: s/addFeature/addAttribute/ :-(

#3 Updated by Martin Dobias about 12 years ago

addAttribute() method expects QVariant instance, not QString. So this should work:

feat.addAttribute (x, QVariant(y))

The difference in behaviour is probably due different SIP/PyQt4 versions on your systems: one might support automatic conversion from QString to QVariant, while the other doesn't.

#4 Updated by luca76 - about 12 years ago

  • Resolution set to worksforme
  • Status changed from Open to Closed

Ok. Using QVariant fixes the problem. Thank you to all.

#5 Updated by luca76 - about 12 years ago

Ubuntu Karmic has QT 4.5.2, while Jaunty 4.5.0.

With Karmic QVariant is optional, while Jaunty is mandatory.

Also available in: Atom PDF