[Qgis-developer] Getting and setting attribute field names and values for vector layers

Ole Nielsen ole.moller.nielsen at gmail.com
Tue Dec 6 22:55:06 EST 2011


Thanks for that - I wouldn't have known where to look. But this does the
trick - although for my application I did have a need to explicitly convert
the QString types to unicode.
I looked at methods toAscii() and toLatin1() but neither produced the
python string types I was expecting. Can you comment on that?

Many thanks again
Ole


On Wed, Dec 7, 2011 at 8:06 AM, Giuseppe Sucameli <sucameli at faunalia.it>wrote:

> Hi Ole,
> to get the field names you have to use the vector data provider.
>
> If vl contains the vector layer than:
>
>
> provider = vl.dataProvider()  # QgsVectorDataProvider
> fields = provider.fields()  # QMap<int, QgsField>
>
> ... # vl.select(), feat = QgsFeature(), while vl.nextFeature(feat):
>
>    attrs = feat.attributeMap()
>    for (k,attr) in attrs.iteritems():
>        name = fields[ k ].name()
>        print "%s: %s" % (name, attr.toString())
>
>
> Regards.
>
> --
> Giuseppe Sucameli
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/qgis-developer/attachments/20111207/bd6ca947/attachment-0001.html


More information about the Qgis-developer mailing list