Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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

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, first attribute field has by default Real data type, so it recognizes only numbers. I converted that field to String using command:

fields[0].setType(10)

btw:

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 data type for new attribute?

share|improve this question
    
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 Jun 18 '12 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 Jun 18 '12 at 18:52
add comment

1 Answer 1

up vote 3 down vote accepted

Changing of attribute types simply isn't supported.

You can only QgsVectorLayer::addAttribute or QgsVectorLayer::deleteAttribute. Those two will only work while editing (ie. 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.

share|improve this answer
    
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 Jun 19 '12 at 8:33
    
Yes, that's the way. –  jef Jun 19 '12 at 10:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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