0

I'm trying to label vector feature:

#############################
palyr = QgsPalLayerSettings()
layer = iface.activeLayer()
palyr.enabled = True
palyr.fieldName = 'Txt'
palyr.writeToLayer(layer)
layer.triggerRepaint()
############################

With this code I get an error:

'QgsPalLayerSettings' object has no attribute 'writeToLayer'

I'm working in Qgis 3.2.

Can someone help me with this.

0

1 Answer 1

2

The API for labeling has changed significantly in QGIS 3.x, and the old "writeToLayer" methods are no longer supported.

Take a look at QgsVectorLayer.labeling(), and the class QgsVectorLayerSimpleLabeling. An adaption of your code would be something like:

label_settings = QgsPalLayerSettings()
label_settings.drawLabels = True
label_settings.fieldName = 'Txt'

layer = iface.activeLayer()
layer.setLabeling(QgsVectorLayerSimpleLabeling(label_settings))
layer.triggerRepaint()
1
  • Nice, it works perfectly. I just added this line before triggerRepaint: layer.setLabelsEnabled(True) Thnx.
    – Torq
    Aug 9, 2018 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.