Friday 9 June 2017

How to hide more than one QGIS attribute field at a time?


Using QGIS 2.14.1, I use the Layer Properties > Fields > Edit Widget to hide the display of an attribute in the attribute table, as shown in this screenshot:



enter image description here


Is it possible to hide multiple attributes all at once without the time-consuming process of going through the Edit Widget one field at a time?



Answer



You can do this via the python interface. Start the Python Console from the plugin menu.


As an example, this code will set all 10 fields of the first layer to "Hidden":


# get map canvas object
mc = iface.mapCanvas()

# get the first layer (layer 0)
l = mc.layers()[0]


# get the edit form configuration
f = l.editFormConfig()

# loop over 0 to 9, and set fields to "Hidden"

[f.setWidgetType(i, "Hidden") for i in range(10)]

# outputs: [None, None, None, None, None, None, None, None, None, None]


It should be possible to get the number of fields from the layer info somehow so you can replace the 10 with something that works with however many fields you have...


If you want to set some edit forms to "Hidden", then get the f as above and just do f.setWidgetType(3,"Hidden") to set the fourth (python starts at 0) field to Hidden. Repeat for all the fields you want.


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...