4

In QGIS Python, how to get alias of field. In class QgsField, i can not find method to do this.

lay = iface.activeLayer()
provider = lay.dataProvider()
fields = provider.getFields()

for field in fields:
    print field.[SOMETHING]
0

2 Answers 2

4

For QGIS v3.x:

If you've got a QgsVectorLayer object

You can get a Python dictionary of aliases calling:

   layer.attributeAliases()

Which gives you something like:

   {'type': 'Municipality Type', 'name': 'Municipality Name', ...}

If you have a specific field index and you only want to get its alias, you can use:

   layer.attributeAlias( field_index )

Which gives you something like:

   'Municipality Name'

If you've got a QgsFields object

(As explained in the answer by @signedav)

You can get a Python list of aliases calling:

   aliases = [field.alias() for field in layer_fields]

Which gives you something like:

   ['Municipality Type', 'Municipality Name', ...]

If you have a specific field index and you only want to get its alias, you can use:

   layer_fields.field( field_index ).alias()

Which gives you something like:

   'Municipality Name'
0
3

It's supported meanwhile (since 3.0):

lay = iface.activeLayer()
field_aliases = [field.alias() for field in lay.fields()]

https://qgis.org/pyqgis/3.2/core/Field/QgsField.html#qgis.core.QgsField.alias

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.