I am using python for QGIS 2.0.1 and I have a vector layer of points representing the positions of state labels. I want to display the 'state abbreviation' attribute on the corresponding point for each feature.

Does anyone have an example using QgsPalLabeling to do this? Any other suggestions are also appreciated.

This is a fairly simple process using the new Labeling tab of the GUI, but I have not found any documentation to make this work programmatically using pyQGIS.

  • i have the same problem, how can i apply this code for a vector layer of polygons? thanks – Nora 27 Jul 23 '14 at 12:51

Just figured this one out thanks to a modification of an older post by dakcarto.

Basically the python would need to look like this (replace everything in bold):

palyr = QgsPalLayerSettings()
palyr.readFromLayer(My Vector Layer)
palyr.enabled = True
palyr.fieldName = 'My Attribute Field Name'
palyr.placement= QgsPalLayerSettings.OverPoint
palyr.writeToLayer(My Vector Layer)

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.