I have a problem with predefined values and their order in 'value map' field. Wnen I tried this How to create predefined values in a field in PyQGIS? it works, but I'm not able to keep order (even when I use OrderedDict instead of dict). The second problem is how to insert also empty value.

    val_map = OrderedDict()
    for value,label in sorted(val['values'].items()):
        val_map[label.decode("utf-8")] = label.decode("utf-8")
    vlayer.setEditorWidgetV2( index, 'ValueMap' )
    vlayer.setEditorWidgetV2Config(index, val_map)

result: OrderedDict([(u'silnice I.t\u0159', u'silnice I.t\u0159'), (u'silnice II.t\u0159', u'silnice II.t\u0159'), (u'silnice III.t\u0159', u'silnice III.t\u0159'), (u'MK I.t\u0159', u'MK I.t\u0159'), (u'MK II.t\u0159', u'MK II.t\u0159'), (u'MK III.t\u0159', u'MK III.t\u0159'), (u'MK IV.t\u0159', u'MK IV.t\u0159'), (u'UK', u'UK')])

But you can see, that there is alphabetical order in value map enter image description here

  • 2
    Did you try your OrderedDict.keys() and orderedDict.values()? It would be good if you post the code that didn't work for you so that we know what went wrong. – wondim Jun 23 '17 at 15:19

Your Answer

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

Browse other questions tagged or ask your own question.