2

In QGIS 3.28 I am trying to align my labels along line features but cannot get them in curved mode. I tried:

layer = iface.activeLayer()
layer.placement = Qgis.LabelPlacement.Curved
settings = QgsPalLayerSettings()
settings.isExpression = True
settings.fieldName = "NAME"

settings.displayAll = True
layer.setLabeling(QgsVectorLayerSimpleLabeling(settings))
layer.setLabelsEnabled(True)
layer.triggerRepaint()

but this did not work and the labels still appear as "Parallel" when checking layer properties.

Is there any way to change this? I am using this in the context of rule-based labeling but it simply seems not to work. What do I miss here?

0

1 Answer 1

3

The QgsVectorLayer class has no method like placement. However, the QgsPalLayerSettings does.

I suppose you are trying to apply this LabelPlacement mode.

Just add this settings.placement = Qgis.LabelPlacement.Curved statement into your code:

from qgis.utils import iface
from qgis.core import Qgis, QgsPalLayerSettings, QgsVectorLayerSimpleLabeling

layer = iface.activeLayer()

settings = QgsPalLayerSettings()
settings.isExpression = False
settings.fieldName = "NAME"
settings.displayAll = True
settings.placement = Qgis.LabelPlacement.Curved

labeling = QgsVectorLayerSimpleLabeling(settings)

layer.setLabeling(labeling)
layer.setLabelsEnabled(True)
layer.triggerRepaint()

Note: There is no need in settings.isExpression = True, because "NAME" is a simple field without any add-ons.

I am using QGIS 3.28.10-Firenze.


References:

1
  • 1
    Yep - that was the solution. Excellent thanks! Nov 10, 2023 at 7:47

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.