4

I am trying to set the edit type of a vector layer to use a value relation, by pyqgis. So my question is exactly the same as this, except now it is qgis 2.14. I found that setEditForm is deprecated and this method is recommended instead:

vlayer.editFormConfig().setWidgetType(index,"ValueRelation")

setWidgetType sets the edit widget correctly but then I have the same problems as in the question regarding qgis 2.0. I.e. python commands are processed without errors or warnings but the when I check the properties in QGIS, the widget settings are empty:

vlayer.valueRelation(index).mLayer = target_layer.id()
vlayer.valueRelation(index).mKey = key_field
vlayer.valueRelation(index).mValue = value_field
vlayer.valueRelation(index).mAllowNull = True
vlayer.valueRelation(index).mOrderByValue = True

How do I define the value relation by pyqgis in qgis 2.14?

1 Answer 1

5

The options for the widget configurations are passed in as dict.

It's best to inspect first what a manually configured (and working) configuration looks like.

vlayer.editFormConfig().widgetConfig(index)

With this command you will see, that a ValueRelation accepts the following configuration options:

  • FilterExpression
  • Layer
  • UseCompleter
  • AllowMulti
  • AllowNull
  • OrderByValue
  • Value
  • Key

Now simply do it the other way round:

cfg = dict()
cfg['Layer'] = target_layer.id()
cfg['Key'] = key_field
cfg['Value'] = value_field
cfg['AllowNull'] = True
cfg['OrderByValue'] = True
# Add more if you like
vlayer.editFormConfig().setWidgetConfig(index, cfg)
2
  • This is a very clear and useful answer that solved my problem. Thank you Matthias!
    – joskal
    Commented Aug 12, 2016 at 18:56
  • can you provide the link of the documentation like what does UseCompleter do ? and how to use it ? Commented May 26, 2017 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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