4

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?

3 Answers 3

4

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.

3
  • Perfect! Thanks so much. I didn't know anything about Python before this question, and I learned a little by observing your code. I became curious about how to select the layer. I tried to select the second layer in the table of contents by changing
    – Stu Smith
    Commented May 9, 2016 at 5:25
  • l = mc.layers()[0] to l = mc.layers()[1], but none of the fields were changed to Hidden. Weird...
    – Stu Smith
    Commented May 9, 2016 at 5:31
  • setWidgetType was deprecated many releases back. Now you have to use setWidgetConfig, which takes a name, not an index. But I can't figure out how to get a list of widget names.
    – Llaves
    Commented May 14, 2021 at 23:21
0

It is not included in QGIS itself.

Note that "l = mc.layers()[0]" refers to the first layer that is visible (ticked in layers panel) - If you always leave the zero be in the script you can just tick/untick layers to make the relevant layer being the first visible and the run the script.

0
0

Couple of lines to set all field widgets to 'Hidden' for active (selected) layer, tested on QGIS 3.24 in Python Console:

layer = iface.activeLayer()
fields = layer.fields()
for i in range(0, fields.count()):
    f = fields.field(i)
    hidden_setup = QgsEditorWidgetSetup('Hidden', f.editorWidgetSetup().config())
    layer.setEditorWidgetSetup(i, hidden_setup)

In words get active layer in QGIS, and get it's fields. For range from 0 to fields count get field by index. Use type 'Hidden', and field's current widget setup configuration to create new hidden setup. Finally set it to according layer widget setup by field index.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.