I'm trying to find how to get with the QGIS Python API the layer field name that contain the rotation angle used by a layer to rotate its labels?

I know that I can obtain the field name containing the text used to label features this way:

palyr = QgsPalLayerSettings()
f = palyr.fieldName

but I can't figure out how to obtain the rotation field name when setted in the "Data defined" section... The QgsPalLayerSettings class contains a lot of methods related to the manipulation of "DataDefined" properties.

1 Answer 1


Access to the data defined properties was just fixed today (previously caused a crash). Following your example code, you can access it like so (most QgsPalLayerSettings properties are accessed directly):

layer = iface.activeLayer()
palyr = QgsPalLayerSettings()

# get currently set data definitions as dict of 
#   {QgsPalLayerSettings.DataDefinedProperties: QgsDataDefined}
#   e.g. {18: <qgis.core.QgsDataDefined object at 0x1237daa70>, 10: ...}
ddp = palyr.dataDefinedProperties

# get a data definition from those currently set
ddft = None
if QgsPalLayerSettings.FontTransp in ddp:
    ddft = ddp[QgsPalLayerSettings.FontTransp]

# or try to get a data definition directly (returns None if not found)
ddfc = palyr.dataDefinedProperty(QgsPalLayerSettings.FontCase)
ddfc_field = ''
if ddfc is not None:
    ddfc_field = ddfc.field()

# you can update the data definition as well
ddfc = palyr.dataDefinedProperty(QgsPalLayerSettings.FontCase)
if ddfc:
    ddfc.setField('myfield')  # which would need to be a valid field

# you can also add a new data definition
if not QgsPalLayerSettings.Size in ddp:
    # add an active definition that defines to use an expression of 36 
    # (i.e. no attribute field is mapped)
    palyr.setDataDefinedProperty(QgsPalLayerSettings.Size, True, True, '36', '')

# whenever you update/add/remove a data definition,
# from your QgsPalLayerSettings object, if you want to
# save it to the project instance and see results on canvas

In order to know what DataDefinedProperties enums to use, review the QgsPalLayerSettings docs. Consult the docs on QgsDataDefined to see how to access/manipulate it.


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.