2

I have a custom UI form made with Qt Designer with a couple of dropdown list, so I can easily edit a layer. I use a Value Relation on those fields to filter the displayed data. The problem is the function current_value() is not working when I use a provided ui file, but it works perfectly when I use the default autogenerated form. I've tried this both programatically and through the Layer Properties interface with the same result. I guess the problem is the function current_value() doesn't find the field to be filtered by, but I don't know how to point to that field correctly on my custom form. I want to set the state first, and then show only the towns that are on that state.

The form widget name is the same as the field name. I tried this both in QGIS 3.4.3 and 3.6.1. This is the code I used, though it worked fine on default form. Im I missing something or is this a bug ?

myLayer = QgsVectorLayer(uri.uri(), 'myLayer', 'postgres')
states = QgsVectorLayer(uri.uri(), 'states', 'postgres')
towns = QgsVectorLayer(uri.uri(), 'towns', 'postgres')

stateIndex = myLayer.fields().indexFromName('state')
townIndex = myLayer.fields().indexFromName('town')

myLayer.setEditorWidgetSetup(stateIndex, QgsEditorWidgetSetup('ValueRelation', {
    'Layer': states.id(),
    'Key': 'id',
    'Value': 'name'
    }
))

myLayer.setEditorWidgetSetup(townIndex, QgsEditorWidgetSetup('ValueRelation', {
    'Layer': towns.id(),
    'Key': 'id',
    'Value': 'name',
    'FilterExpression': '"fk_state" = current_value(\'state\')'
    }
))

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.