I would like to make an open file dialog to appear in a field in the attribute table of a vector layer. I guess I need to set widget type to 'Attachment' for that field and to check 'Display button to open file dialog'.

I also guess I should use the QgsEditorWidgetSetup class, but I am not sure how to set the configuration options. I wrote something like:

setup=QgsEditorWidgetSetup('Attachment',{???}) #What should I write in {}?

I have found examples for Date/Time and Value Map types, but what about Attachment?


Solving this particular question

The attachment widget is of type 'ExternalResource'. This is an example for you to configure it:

FIELD = "field_name"

editor_widget_setup = QgsEditorWidgetSetup(
        'FileWidget': True,
        'DocumentViewer': 0,
        'RelativeStorage': 0,
        'StorageMode': 0,
        'DocumentViewerHeight': 0,
        'FileWidgetButton': True,
        'DocumentViewerWidth': 0,
        'FileWidgetFilter': ''
index = layer.fields().indexFromName(FIELD)
layer.setEditorWidgetSetup(index, editor_widget_setup)

More generic answer (recommendation)

As you can see, neither type nor config params for QgsEditorWidgetSetup are intuitive, and I guess you cannot find them currently in the docs. However, you could do some reverse-engineering:

  1. Configure your field widget using the GUI.
  2. Get the type and config for such widget:

    ews = layer.editorWidgetSetup(field_index)  
    print("Type:", ews.type())
    print("Config:", ews.config())

This way you can discover config keys and widget types.

  • This works fine! – Giovanna De Filippis Jan 3 '20 at 14:50
  • Thank you for your detailed reply! Indeed, I got crazy while looking for the detailed documentation, but your recommendation is more feasible. – Giovanna De Filippis Jan 3 '20 at 14:52

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.