1

I need to create a widget were some fields of a feature are listed and are editable.

I used QgsAttributeForm for this, but this widget is too big and has to many fields.

I need to provide a QCheckBox as well before every field to mark it as selected or deselected.

I took a look at the QgsFeature class and its fields() method. The QgsField class has a type() Method for determing the type of a field, but unfortunately I have not yet found an easy to create a widget from a field type.

I took a look at the QgsFieldWrapper classes, but they don't seem to work in PyQGIS because they are abstract classes.

Is there an easy way to create a default widget from a feature field, e.g. widget = FieldWidgetWrapper(field) that I can use to generate these controls?

I know I can create custom UIs for AttributeForms, but this will not work, because I need to be able to have more than one feature in this widget represented by specific fields.

Example

Currently I display everything in LineEdits, but this is not working, especially for DateTime objects. It would be cool if the widgets can be created automatically from a field like in the QgsAttributeForm class...

1

Ok I managed to create a Widget now, but I'm unable to add it to a layout. I created this test code:

reg: QgsEditorWidgetRegistry = QgsGui.editorWidgetRegistry()
w = QWidget()
layout = QVBoxLayout()
layout.addWidget(QPushButton('Test'))

fldIdx = fields.lookupField('nummer')
if fldIdx < fields.count() and fldIdx >= 0:
    widgetSetup: QgsEditorWidgetSetup = reg.findBest(flurstueckLayer, 'nummer')
    eww = reg.create(widgetSetup.type(), flurstueckLayer, fldIdx, widgetSetup.config(), None, w)
    eww.setFeature(feat)
    formWidget = QgsAttributeFormEditorWidget(eww, widgetSetup.type(), None)
    formWidget.setMode(QgsAttributeFormWidget.DefaultMode)
    layout.addWidget(formWidget)

w.setLayout(layout)
w.show()

it renders the widget and also adds the value form the feature attribute, but it is not managed by my QVBoxLayout():

enter image description here

I also tried to add the form to the layout (formWidget.form()), but it also doesn't work.

Am I using the correct classes and methods? I took this code from the AttributeForm C++ code.

How can I add this to my layout so it will be correctly displayed?

  • When I set the Mode to Search: formWidget.setMode(QgsAttributeFormWidget.SearchMode), I can see a searchWidget-Combobox, which is embedded correctly in the layout. The editor still is not added to the layout. – TheGrudge Jan 15 '19 at 14:58

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.