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

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

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

For QGIS 2.x:

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

For QGIS 3.x:

layer = iface.activeLayer()
idx = layer.fields().indexFromName('fieldName')
for dicts in layer.editorWidgetSetup(idx).config().values():
  • 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
  • @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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.