Skip to main content
2 of 3
added 2 characters in body
Taras
  • 34.1k
  • 4
  • 73
  • 148

If you need to get the fields of a layer you can use QgsVectorLayer::pendingFields() in Python like so:

fields = layer.pendingFields()

which will give you something like this:

{0: <qgis.core.QgsField object at 0x46d9d40>, 
1: <qgis.core.QgsField object at 0x46d9b00>, 
2: <qgis.core.QgsField object at 0x46d9cb0>, 
3: <qgis.core.QgsField object at 0x46d9ef0>, 
4: <qgis.core.QgsField object at 0x46d9dd0>}

Which you can then go though get the name, data type, and length:

for field in fields.itervalues():
    print "Name:%s Type:%s, Length:%s" % ( field.name(), field.typeName(), field.length())

Name:Seg_Par Type:String, Length:10
Name:Lot_num Type:String, Length:5
Name:Plan_num Type:String, Length:10
Name:Tenure Type:String, Length:2
Name:Area_ha Type:Real, Length:14
Nathan W
  • 35k
  • 5
  • 99
  • 148