1

I'm working on Python Console with Qgis vers. 3.36. I would place some labels below some points. I saw just in this forum other question about this issue, but I think the way how to act in this case is changed. Here is the code:


def labelGraph(layerInInput):
    labelSet  = QgsPalLayerSettings()
    text_format = QgsTextFormat()
    text_format.setFont(QFont("Arial", 12))
    text_format.setSize(10)
    labelSet.setFormat(text_format)
    labelSet.enabled = True
    labelSet.placement = Qgis.LabelPlacement.AroundPoint
    etichette = QgsVectorLayerSimpleLabeling(labelSet)
    layer_settings = QgsVectorLayerSimpleLabeling(labelSet)
    layerInInput.setLabelsEnabled(True)
    layerInInput.setLabeling(etichette)
    layerInInput.triggerRepaint()

I tried, instead "AroundPoint", also "Horizontal", "OverPoint", "OrderedPositionsAroundPoint", but nothing: the result is not that I wish.

0

1 Answer 1

0

You need to use OverPoint placement and set the quadrant.

In QGIS versions >= 3.38 you can use the QgsLabelPointSettings class:

labelSet.placement = Qgis.LabelPlacement.OverPoint
lbl_pt_set = QgsLabelPointSettings()
lbl_pt_set.setQuadrant(Qgis.LabelQuadrantPosition.Below)
labelSet.setPointSettings(lbl_pt_set)

In versions < 3.38 just use the quadOffset attribute:

labelSet.placement = Qgis.LabelPlacement.OverPoint
labelSet.quadOffset = Qgis.LabelQuadrantPosition.Below

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.