4

Currently, for a python plugin for QGIS 2.8.3, I get the list of fields for a layer using pendingFields as mentioned in this thread:

fields = self.layer.pendingFields()

I now have a few hidden fields (set via the fields tab in layer properties). How can I get a list of fields that are not hidden?

I see the possibility to get Edit Type, but am not sure where to go from here :

EditType { 
  LineEdit, UniqueValues, UniqueValuesEditable, ValueMap, 
  Classification, EditRange, SliderRange, CheckBox, 
  FileName, Enumeration, Immutable, Hidden, 
  TextEdit, Calendar, DialRange, ValueRelation, 
  UuidGenerator, Photo, WebView, Color, 
  EditorWidgetV2 
}
3

The vector layer object has a editorWidgetV2ByName method which allows you to pass in a field name and get back the the name of the Edit Widget as a string. For example:

>>> layer.editorWidgetV2ByName("fieldname")
'Hidden'

Alternately there is the editorWidgetV2 method which allows you to pass in a field index:

>>> layer.editorWidgetV2(0)
'Hidden'

The list of names for different widgets can be found with the documentation for setEditorWidgetV2, which incidently can be used to set the widget for a field given the index, like so:

>>> layer.setEditorWidgetV2(0, "Hidden")

So to get a list of fields which aren't hidden you can use the following snippet:

fields = [
    field for field in layer.pendingFields()
    if layer.editorWidgetV2ByName(field.name() != "Hidden")
]

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.