1

I'm trying to set properties of QgsVectorLayer using QGIS 3. I want to put labels with "Offset from point" and define "Offset X" and Offset Y". I saw on the documentary that there is a class "setQuadrantPosition" from QgsPalLayerSettings but i don't know how to use it. Here is my code :

layer=iface.activeLayer() layer_settings = QgsPalLayerSettings() layer_settings.fieldName = "Name" layer_settings.enabled = True layer_settings = QgsVectorLayerSimpleLabeling(layer_settings) layer.setLabelsEnabled(True) layer.setLabeling(layer_settings) layer_settings.setQuadrantPosition(5) layer.triggerRepaint() qgis.utils.iface.layerTreeView().refreshLayerSymbology(layer.id())

Here is a picture of what i want to set up :

0

You can use the following to set up your label placement properties, see comments:

layer = iface.activeLayer()
layer_settings = QgsPalLayerSettings()
layer_settings.fieldName = "Name"

# Placement: Cartographic = 6; Around point = 0; Offset from point = 1
layer_settings.placement = 1
# Quadrant position: QuadrantAboveLeft = 0; QuadrantAbove = 1,...
layer_settings.quadOffset = 5
# Offset X
layer_settings.xOffset = 3.0
# Offset Y
layer_settings.yOffset = 3.0

layer_settings = QgsVectorLayerSimpleLabeling(layer_settings)
layer.setLabelsEnabled(True)
layer.setLabeling(layer_settings)
layer.triggerRepaint()
iface.layerTreeView().refreshLayerSymbology(layer.id())
  • 1
    Thank you so much ! Really helpfull for me, thanks again ! – Valentin L. Jun 11 at 9:14
  • @ValentinL. - Most welcome! Glad it helped again :) – Joseph Jun 11 at 10:35

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.