1

How could I use layer.setEditType() method in QGIS for a custom form widget? Cusom form elements are well explained by Matthias Kuhn here.

I was able to change the existing default widget types. For instance, layer.setEditType(idx, layer.ValueMap) but when I tried the same on a custom widget, it doesn't work. How could I do it?

2

The method setEditType is really old and no longer recommended (and will disappear with QGIS 3.0).

If you have a look at the API documentation, it redirects to the appropriate successor: https://qgis.org/api/classQgsVectorLayer.html#a2d1e5058f8b7cc5c905813485847bc81

layer.editFormConfig().setWidgetType(idx, "ValueMap")       

or

layer.editFormConfig().setWidgetType(idx, "YourCustomWidgetId")

Closely related is also the method setWidgetConfig

layer.editFormConfig().setWidgetConfig(idx, { 'rule': '[A-Z].*' })
11
  • Thank you very much for the quick answer. I tested, it works well for others but for the custom widget, it shows a python key error for this line. def writeConfig(self, config, elem, doc, layer, idx): elem.setAttribute('rule', config['rule']) I didn't make any change on it.
    – wondim
    Jul 5 '16 at 3:29
  • I fixed the above issue with `elem.setAttribute(idx, config['rule'])' .However, I have an issue of making the custom widget usable. Sometimes it says the widget type is not usable and sometimes it shows the red color on the line edit by default and when I type the right pattern, it doesn't change.
    – wondim
    Jul 5 '16 at 6:00
  • Does it work when you set it manually in the vector layer properties dialog? Jul 5 '16 at 6:47
  • Yes, it works when I set it in the properties dialog.
    – wondim
    Jul 5 '16 at 7:33
  • Make sure to read the docs ;) answer updated Jul 5 '16 at 7:40

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.