Saturday, 4 July 2015

Implementing layer.setEditType for custom widgets in PyQGIS?


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?



Answer



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].*' })

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...