3

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?

3

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)
  • This is a very clear and useful answer that solved my problem. Thank you Matthias! – joskal Aug 12 '16 at 18:56
  • can you provide the link of the documentation like what does UseCompleter do ? and how to use it ? – Abhijit Gujar May 26 '17 at 13:30

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.