It is simpler than with version 1.8:
layer = qgis.utils.iface.activeLayer()
fields = layer.pendingFields()
field_names = [field.name() for field in fields]
or in one line
field_names = [field.name() for field in layer.pendingFields() ]
an after
for elem in layer.getFeatures():
print dict(zip(field_names, elem.attributes()))
{u'adip': 17, u'dipdir': 130, u'tdip': 29}
{u'adip': 55, u'dipdir': 325, u'tdip': 75}
.....
Generally to explore a new function, I use the dir() or the see module to examine what's inside:
dir(fields)
['FieldOrigin', 'OriginEdit', 'OriginJoin', 'OriginProvider', 'OriginUnknown', '__class__', '__delattr__', '__delitem__', '__dict__', '__doc__', '__format__', '__getattribute__', '__getitem__', '__hash__', '__init__', '__len__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'append', 'at', 'clear', 'count', 'extend', 'field', 'fieldOrigin', 'fieldOriginIndex', 'indexFromName', 'isEmpty', 'remove', 'size', 'toList']
or
see(fields)
[] hash() help() len()
repr() str() .FieldOrigin() .OriginEdit
.OriginJoin .OriginProvider .OriginUnknown .append()
.at() .clear() .count() .extend()
.field() .fieldOrigin() .fieldOriginIndex()
.indexFromName() .isEmpty() .remove() .size()
.toList()
And you can see that there is a field() function
so
print fields.field(0)
qgis.core.QgsField object at 0x163E39C0
and a field has a name, a type, a precision,...:
see(fields.field(0))
< <= == != >
>= hash() help() repr()
str() .comment() .displayString() .length()
.name() .precision() .setComment() .setLength()
.setName() .setPrecision() .setType() .setTypeName()
.type() .typeName()
so:
fields.field(0).name()
u'adip'
fields.field(0).type()
2
fields.field(0).typeName()
u'Integer'
fields.field(0).precision()
0
....
with .dataProvider()
It is .field()
....
prov = layer.dataProvider()
prov.fields().field(0).name()
u'adip'
prov.fields().field(0).typeName()
u'Integer'
and
field_names = [field.name() for field in prov.fields()]
Visible layers
You could use this function to get layer names printed:
def get_layers():
# List visible layers (warning: only spatial ones)
layer_list = []
for layer in iface.mapCanvas().layers():
item = layer.name()
layer_list.append(item)
print(layer_list)
get_layers()
Layers in TOC (layers panel / legend / layer tree)
In case that you need all layers in the TOC (not only those that are visible/checked, as mapCanvas().layers()
returns), you can use this function:
def get_toc_layers(group):
# List all layers in a group
for child in group.children():
if isinstance(child, QgsLayerTreeLayer):
print(child.layer().name())
else:
get_toc_layers(child)
root = QgsProject.instance().layerTreeRoot()
get_toc_layers(root)
All registered layers
QGIS can use layers that are part of the QGIS project but are not shown in the TOC. If you want to get those layers listed as well, do this:
def get_all_layers():
# List register layers (even if they aren't in the TOC)
layer_list = []
for k, layer in QgsProject.instance().mapLayers().items():
layer_list.append(layer.name())
print(layer_list)
get_all_layers()
Best Answer
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:Alternately there is the
editorWidgetV2
method which allows you to pass in a field index: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:So to get a list of fields which aren't hidden you can use the following snippet: