0

I am trying to create labels for 3 separate measurements, which I intend to offset in the label placement options so that the text appears above, centered and below a given point. The problem I am encountering though, is that I can't get QGIS to change the label placement mode from 'Around Point' to 'Offset from Point'.

My original code looked like this:

    pal_layer = QgsPalLayerSettings()
    pal_layer.isExpression = True
    pal_layer.fieldName ="""round("%s" * 1.09361)"""  % (fieldName)

    pal_layer.enabled = True
    pal_layer.placement = 1
    pal_layer.quadOffset = 1
    pal_layer.xOffset = 0
    pal_layer.yOffset = offsetY

I would expect pal_layer.placement to set the placement mode to 'Offset from Point' , however, in the current version of QGIS I am running, both pal_layer.placement and pal_layer.quadOffset expect an enum and not an integer. This has confused me, as previous forum posts on a similar topic would suggest that this code should work fine. I have looked at the documentation for Class: QgsPalLayerSettings and I can see that QgsPalLayerSettings.Placement uses an integer but QgsPalLayerSettings.placement does not. I'm not sure if that could be causing an issue? Regardless, I am mainly interested in how to set the label placement mode to 'Offset from Point' via Python.

1 Answer 1

0

Looks like the QgsPalLayerSettings.placement field changed from int to enum in QGIS version 3.26.

If you were using an earlier version, what you have should work (I was able to change placements in 3.22 using similar code to yours).

For versions >= 3.26, you should just be able to update your code with:

pal_layer.placement = Qgis.LabelPlacement.OverPoint

Reference: Qgis.LabelPlacement API reference

1
  • Thank you! This worked perfectly!
    – AB_002
    Oct 17, 2023 at 6:31

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.