1

I'm trying to update the units used in labelling in QGIS 3.0.

The following answer shows how to do that but requires the user to overwrite all the changes:
Labelling line layer using QgsPalLayerSettings() QGIS failed

I want to edit only the label units and nothing else about the label style.

For example, I can edit the line size unit in the following way:

iface.activeLayer().renderer().symbol().setOutputUnit(2)  

Is it possible to edit only the label size units (i.e MM/Pixel etc...) via Python?

1 Answer 1

2

In QGis 2.18, you would be able to do something like this:

iface.activeLayer().setCustomProperty("labeling/fontSizeInMapUnits",True)

However, this is different in QGis 3.0, the structure is nested in more layers and you have to manage the intermediate objects. You need to access the labeling, then the settings, then the inner text format object, modify it, then apply it:

labeling = iface.activeLayer().labeling().clone()
settings = labeling.settings()
format = settings.format()
format.setSizeUnit(QgsUnitTypes.RenderPoints)
settings.setFormat(format)
labeling.setSettings(settings)
iface.activeLayer().setLabeling(labeling)
2
  • Thank you, works perfectly for text size. I tried the same method for label buffer units but QGIS keeps crashing. Any success getting that to work for you? I tired: bufferformat = settings.format().buffer() bufferformat.setSizeUnit(QgsUnitTypes.RenderPixels)
    – Cushen
    Commented Mar 18, 2018 at 22:46
  • 1
    Just use the same logic, with an additional layer of nesting: labeling = iface.activeLayer().labeling().clone() settings = labeling.settings() format = settings.format() buffer = format.buffer() buffer.setSizeUnit(QgsUnitTypes.RenderPoints) format.setBuffer(buffer) settings.setFormat(format) labeling.setSettings(settings) iface.activeLayer().setLabeling(labeling)
    – Leyan
    Commented Mar 18, 2018 at 23:30

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.