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
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 '18 at 10:07

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.