3

Context

I am developing a QGIS plugin using QGIS 3.22.11-Białowieża. One of the functions is to add a layer to the map and format the fields in the attributes form. I want to avoid developing a custom form and just use the "drag and drop designer" layout:

editFormConfig = feature_layer.editFormConfig()
editFormConfig.setLayout(1) 

This is working great so far. I am able to customize my fields as needed and add them to the Attributes Form just fine, for example:

fields = feature_layer.fields()
field_index = fields.indexFromName(field_name)
widget_setup = QgsEditorWidgetSetup('TextEdit', {'IsMultiline': True, 'UseHtml': False})
feature_layer.setEditorWidgetSetup(field_index, widget_setup)
form_config = feature_layer.editFormConfig()
form_config.setLabelOnTop(field_index, True)
feature_layer.setEditFormConfig(form_config)

The Problem

I have a QgsAction (button to open URL) that I would like to add to the Attributes Form.

helpAction = QgsAction(1, 'Open Help URL', help_action_text, None, capture=False, shortTitle='Help', actionScopes={'Layer'})
feature_layer.actions().addAction(helpAction)

This part is also working fine, the Action is added to the layer properly and is available as a widget in the drag-and-drop designer for the Attributes Form. When I manually drag the Action into the Form Layout, it appears as a button and works as expected.

However, I cannot find where to add this to the programmatically to the Attributes Form as I do with the fields. Is this even possible?

Question

How can I add a layer Action (QgsAction) to that layer's Attributes Form programmatically?

1 Answer 1

1

Somehow I missed this in the documentation: https://qgis.org/pyqgis/3.22/core/QgsAttributeEditorAction.html

editorAction = QgsAttributeEditorAction(helpAction, rootContainer)
rootContainer.addChildElement(editorAction)

Works great!

2
  • Can you give further details on the rootContainer please?
    – ThiPa
    Commented Dec 16, 2022 at 13:19
  • 1
    rootContainer = editFormConfig.invisibleRootContainer() see the documentation here Commented Jan 11, 2023 at 21:57

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.