[GIS] Getting features attributes on feature selection in QGIS python

pyqgisqgis-2qgis-plugins

I want to know how to get features from a layer on click event.. using python(m trying to create a plugin).. the below code always show 0 output

layer = iface.activeLayer()
selection = layer.selectedFeatures()
print len(selection)
for feature in selection:
    print feature['NAME']

Best Answer

This works fine for me

layer = iface.activeLayer()

h = layer.selectedFeatures()

for f in h: print f['ID']

Related Question