I have made a custom form with a QgsRelationEditorWidget and with a QToolButton. I want to select a feature in this QgsRelationEditorWidget and press the button to open another form with this selected feature. For it, I need to get this selected feature attributes. The code is a py file.

I think I have to access to the QgsRelationEditorWidget with this code:

childFormWidget = dialog.findChildren(QgsRelationEditorWidget)[0]
childForm = childFormWidget.findChildren(QgsRelationEditorWidget)[0]
featureManager = childForm.featureSelectionManager()
infraManager.selectionChanged.connect(partial(changeSelection, featureManager))

But I have this error:

IndexError: list index out of range.

Also, I don't know how to access the attributes with featureManager.

How can I do it? How can I reach to the selected feature?


In case someone needs it, finally I understood how does QgsIFeatureSelectionManager work. This code takes the selected features ids from a QgsRelationEditorWidget and passes it to the function "changeSelection" when we change the selection in widget.

childFormWidget = dialog.findChildren(QgsRelationEditorWidget)[0]
featureManager = childFormWidget.featureSelectionManager()

Then we define the changeSelection function. Here we access to the selected features by:

def changeSelection(deselected, selected):
    idact= feat[0]

Now with the feature's id, we can do all we need. In this case, I only select one feature en QgsRelationEditorWidget (feat[0])

| improve this answer | |

After changing the code as follow:

childFormWidget = dialog.findChildren(QgsRelationEditorWidget)[0]
featureManager = childFormWidget.featureSelectionManager()
feat=featureManager.selectedFeatureCount ()     

I get the correct value of feat, in this case, 1. But I still can not get the attributes of the selected feature.

| improve this answer | |

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.