mercredi 1 avril 2009

QGIS routine-get the fields!

Imagine you'd like to create a plugin that searches objects depending on their attributes.You'd like to display the layer fields in a combobox but how to get them?

The code shown below is often used in plugins and allows you to get the curent layer's fields.
mc=iface.getMapCanvas()
layer=mc.currentLayer()
provider=layer.getDataProvider()
fieldmap=provider.fields()
fieldmap is a list of values containing your field names.
If you want to get the name of the ith field, just type fieldmap[i] in the QGIS python console.
Maybe now you've learned how to get the layer fields, you'd like to know their values. It'll be the purpose of the next post.