Bug report #9773

Issue with Memory Provider example code in PyQGIS Cookbook

Added by Evan Curhan about 10 years ago. Updated about 9 years ago.

Category:Documentation and Help
Affected QGIS version:2.2.0 Regression?:No
Operating System: Easy fix?:No
Pull Request or Patch supplied:No Resolution:invalid
Crashes QGIS or corrupts data:No Copied to github as #:18315


The example code given in the PyQGIS cookbook re: Memory Provider is not entirely functional in newer versions of QGIS (2.0 and above). This section of the code in particular was causing bugs:

# add fields
pr.addAttributes( [ QgsField("name", QVariant.String),
QgsField("age", QVariant.Int),
QgsField("size", QVariant.Double) ] )

I was getting error messages that QGIS was not able to recognize values of type string for fields that I specified as string (i.e. if I were to get an error message from the above that "name," a string, was an unexpected type). I instead had to use the really ugly syntax of writing out all the fields in a URI string, the other option provided above this example in the cookbook:

Additionally, the cookbook did not state anywhere the need for the following import
from PyQt4.QtCore import *
from PyQt4.QtGui import *

statements to use these functions. This might help in the future for beginners like me.



#1 Updated by Alexander Bruy about 9 years ago

  • Resolution set to invalid
  • Status changed from Open to Rejected

This code snippet, not complete example and this is users responsibility to import necessary modules

Also available in: Atom PDF