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.

1

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()
  • Nice, it works perfectly. I just added this line before triggerRepaint: layer.setLabelsEnabled(True) Thnx. – Torq Aug 9 '18 at 10:07
0

I'm doing the above steps in standalone script or python console with current master and can't achieve to display labels. When opening the layer properties, they are all set. Closing layer properties without changing any setting displays then correctly the labels. Is there something more to add after those steps?

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.