I've already asked a question about how to access attribute data types when developing a plugin for QGIS, using PyQGIS. And one helpful member said I should use the pendingFields() method on the current layer. Here is the link to that query: Accessing attribute data type in PyQGIS

Indeed I could access them but when I tried to change the data type with setType() and setTypeName() functions, it didn't work. When I queried the objects with type() and typeName() they showed the new data types I have given them, but when I wanted to enter new values for some feature I realized that this layer doesn't recognize new data types.

For example, the first attribute field has by default Real data type, so it recognizes only numbers. I converted that field to String using the command:



fields = currentLayer.pendingFields()

But the field still acts like before and doesn't recognize letters, only numbers. How to change data types? Is it even possible? Or you can only define the data type for a new attribute?

  • The description of QgsVectorLayer::pendingFields() is 'returns field list in the to-be-committed state', which I take to mean you need to exit from editing the layer for the changes to be written from memory back to the data source. Are you programmatically entering edit mode for the layer, making those field changes, then exiting the editing mode?
    – dakcarto
    Commented Jun 18, 2012 at 18:14
  • It appears you can't make field type changes in the GUI (at least for a shapefile source). The API may not let you make the changes either, but also may not give you an error on attempting to do so. Your best bet may be to duplicate the field, i.e. create new field and copy features over, which means you probably need to duplicate the shapefile by directly using osgeo.ogr module in Python. See: gis.stackexchange.com/questions/7436/…
    – dakcarto
    Commented Jun 18, 2012 at 18:52

1 Answer 1


Changing attribute types simply isn't supported.

You can only QgsVectorLayer::addAttribute or QgsVectorLayer::deleteAttribute. Those two will only work while editing (i.e. between QgsVectorLayer::startEditing() and QgsVectorLayer::commitChanges()) and then QgsVectorLayer::pendingFields() will reflect those changes and so will the UI.

When not editing pendingFields() returns the fields of the vector data provider.

  • OK, thanks, this is what I needed to know! If i can't change the data type I'll manage some workaround or I'll wont implement that feature. I have one more question. If I use QgsVectorLayer::addAttribute it is best to use it with QgsField, right? For example, first I define QgsField (type, length, precision...) and then add it as attribute?
    – kyuss
    Commented Jun 19, 2012 at 8:33
  • Yes, that's the way.
    – jef
    Commented Jun 19, 2012 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.