2

I have a python module, where a QGIS memory vector layer is created, within the same method that creates this layer I have constructed labels using:

text_format = QgsTextFormat()
label = QgsPalLayerSettings()
label.fieldName = 'Description'
label.enabled = True
label.setFormat(text_format)
labeler = QgsVectorLayerSimpleLabeling(label)
layer.setLabelsEnabled(True)
layer.setLabeling(labeler)
layer.triggerRepaint()

And then subsequently from another Python module I add this layer to the QGIS workspace using:

QgsProject.instance().addMapLayer(layer)

The labels are not showing in QGIS, however, if I open the "Labels" dialogue from within QGIS and press "Apply" then the labels show. So I assume that the label field and text is set properly.

I have repeated the problem described above in the Python console within QGIS v3.2.2 using the following script:

from qgis.core import *

layer=QgsVectorLayer('MultiLineString?crs=epsg:4326&field=ID:string&field=Description:string', "MultiLines", "memory")
pr=layer.dataProvider()
record = QgsFeature()
record.setAttributes(['1','test'])
record.setGeometry(QgsGeometry.fromWkt('MULTILINESTRING ((1 1, 6 1))'))
pr.addFeature(record)

text_format = QgsTextFormat()
label = QgsPalLayerSettings()
label.fieldName = 'Description'
label.enabled = True
label.setFormat(text_format)
labeler = QgsVectorLayerSimpleLabeling(label)
layer.setLabelsEnabled(True)
layer.setLabeling(labeler)
layer.triggerRepaint()

QgsProject.instance().addMapLayer(layer)

Any suggestions on what I need to add to my python script to have the labels show?

QGIS v3.2.2-Bonn was used.

5
  • Works fine for me (tested on QGIS 3.4 for Win7-64bit). Which version are you using and how are you running your script?
    – Joseph
    Nov 13, 2018 at 10:34
  • I am using QGIS version 3.2.2-Bonn. I am running my script via a QGIS plugin that I created. Nov 13, 2018 at 23:56
  • Do you think it would make a difference if I created the labels after adding the vector layer to the QGIS worksapce? Nov 14, 2018 at 0:01
  • You could try adding the labels after adding the layer and see if it makes a difference. I tested the code using the Python Console so would be interesting to see if, via a plugin, the layer needs to be added first before you can set labels.
    – Joseph
    Nov 15, 2018 at 10:15
  • I have added a python script which gives the same problem when run from the Python Console Nov 16, 2018 at 7:24

2 Answers 2

3

Try defining the label placement as Line:

text_format = QgsTextFormat()
label = QgsPalLayerSettings()
label.fieldName = 'Description'
label.enabled = True
label.setFormat(text_format)

# --
label.placement = QgsPalLayerSettings.Line
# --

labeler = QgsVectorLayerSimpleLabeling(label)
layer.setLabelsEnabled(True)
layer.setLabeling(labeler)
layer.triggerRepaint()

After testing it also seems to work before adding the layer to the canvas in the standalone script.

from qgis.core import *

layer=QgsVectorLayer('MultiLineString?crs=epsg:4326&field=ID:string&field=Description:string', "MultiLines", "memory")
pr=layer.dataProvider()
record = QgsFeature()
record.setAttributes(['1','test'])
record.setGeometry(QgsGeometry.fromWkt('MULTILINESTRING ((1 1, 6 1))'))
pr.addFeature(record)

text_format = QgsTextFormat()
label = QgsPalLayerSettings()
label.fieldName = 'Description'
label.enabled = True
label.setFormat(text_format)

# --
label.placement = QgsPalLayerSettings.Line
# --

labeler = QgsVectorLayerSimpleLabeling(label)
layer.setLabelsEnabled(True)
layer.setLabeling(labeler)
layer.triggerRepaint()

QgsProject.instance().addMapLayer(layer)
1
  • I verified that the solution works and it has solved my original problem. I suspect that there maybe another approach by somehow refreshing the layer as well, but, I wasn't able to solve it. May 6, 2019 at 4:50
3

There was the same problem. QGIS 3.4.8. Tried to change label.placement to QgsPalLayerSettings.Line or QgsPalLayerSettings.Curved or QgsPalLayerSettings.Horizontal. Labels became visible only after running "Labels" dialog. In my case, I used string expression for labeling, not field value. In this case, have to set this flag manually:

layerSettings = QgsPalLayerSettings()
layerSettings.isExpression = True

"Labels" dialog sets this flag on automatically. To find this, I exported style to .qml before and after run dialog and compared results.

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.