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 :
Answer
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())
No comments:
Post a Comment