8

In QGIS 2.18 I read and set label style settings like this:

props = {
    "labeling": "pal",
    "labeling/enabled": "true",
    "labeling/fontFamily": "Arial",
    "labeling/fontSize": "12",
    "labeling/fieldName": "Name",
    "labeling/bufferDraw": True,
    "labeling/bufferSize": "1",
    "labeling/bufferColor": "white"
    }
for k, v in props.items():
    my_layer.setCustomProperty(k, v)

How do I do the equivalent in QGIS 3.0?

1 Answer 1

14

I found the answer for QGIS 3.0:

layer_settings  = QgsPalLayerSettings()
text_format = QgsTextFormat()

text_format.setFont(QFont("Arial", 12))
text_format.setSize(12)

buffer_settings = QgsTextBufferSettings()
buffer_settings.setEnabled(True)
buffer_settings.setSize(1)
buffer_settings.setColor(QColor("white"))

text_format.setBuffer(buffer_settings)
layer_settings.setFormat(text_format)

layer_settings.fieldName = "my_attribute"
layer_settings.placement = 2

layer_settings.enabled = True

layer_settings = QgsVectorLayerSimpleLabeling(layer_settings)
my_layer.setLabelsEnabled(True)
my_layer.setLabeling(layer_settings)
my_layer.triggerRepaint()
1
  • 1
    For those like me, who want to use it with expression: layer_settings.fieldName = "your_expression_here" / layer_settings.isExpression = True Jul 31 at 10:32

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.