6

I have a QgsVectorLayer and right now I'm retrieving it's fields like that:

for index in layer.attributeList():
        print(layer.fields()[index].name(), layer.attributeDisplayName(index))

Now I changed the Attributes Form to "Drag and Drop Designer" defining my own Form Layout.

I need to retrieve the same fields but maintaining the order, which is represented in the QGS project file as <attributeEditorForm> element. So I'm looking for the index of these <attributeEditorField> elements, or directly use this list (instead of attributeList) to get the fields in the order defined in the "Drag and Drop Designer".

0
6
+50

You can get the list of field names in the order you have arranged them (using the Drag and drop designer) in this way:

qgis_layer = iface.activeLayer()
edit_form_config = qgis_layer.editFormConfig()
root_container = edit_form_config.invisibleRootContainer()

for field_editor in root_container.findElements(QgsAttributeEditorElement.AeTypeField):
    print(field_editor.name())

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.