3

I have a vector layer in which attributes have value maps and I need to get the description of the value. I am able to get the values with feature.attributes(), but I haven't found a way to get the description.

layer = canvas.currentLayer()
attributes = feature.attributes()
print attributes[5] # prints 0, but I need Unknown

enter image description here

Taras
  • 32,823
  • 4
  • 66
  • 137
RistoYlem
  • 167
  • 5

1 Answers1

3

You could use the following to get the description of a specific value:

For PyQGIS 3

layer = iface.activeLayer()

idx = layer.fields().indexFromName('fieldName') for dicts in layer.editorWidgetSetup(idx).config().values(): print(list(dicts.keys())[list(dicts.values()).index('0')])

For PyQGIS 2

layer = iface.activeLayer()

idx = layer.fieldNameIndex('fieldName') for description, value in layer.editorWidgetV2Config(idx).iteritems(): if value == '0': print description

Taras
  • 32,823
  • 4
  • 66
  • 137
Joseph
  • 75,746
  • 7
  • 171
  • 282
  • This is kind of what I need, but seems little bit complicated for my final application. Is it possible to get the description directly from the attributes, in the attributes table it shows the description not the value. – RistoYlem Sep 26 '17 at 10:48
  • @RistoYlem - The attribute table only stores the values within the .dbf file (in this case 0). So if you read the table directly, it will only show 0. I'm not sure there's another way to get the description other than to access the value map widget and read the description from there. – Joseph Sep 26 '17 at 11:00
  • Thank you for your help and time, but I manage to do this totally different way. – RistoYlem Sep 26 '17 at 11:21
  • 1
    @RistoYlem - Most welcome and glad you found a solution! Could you please post it as an answer so that others could also benefit? :) – Joseph Sep 26 '17 at 11:23