I want to label a shp layer on two lines with pyQGis. I managed to do it with only one field, but with two or more fields (using concat) it does not work anymore.
Here is the code I wrote:
def labelLine (lyr, fieldNom1, fieldNom2):
# This function shows the labels of 2 fields "fieldName1" and "fieldName2" for a Line layer lyr
label = QgsPalLayerSettings ()
label.readFromLayer (lyr)
label.enabled = True
# we concatenate the two fields
label.fieldName = "concat ('BASE:', fieldName1, '\\ n', 'L =', fieldName2, 'm')"
label.placement = QgsPalLayerSettings.Line
label.bufferDraw = True
label.bufferSize = 1
label.setDataDefinedProperty (QgsPalLayerSettings.S ize, True, True, '8', '')
label.writeToLayer (lyr)

\\ nwith\n? Solabel.fieldName = "concat('BASE:', fieldName1, '\n', 'L =', fieldName2, 'm')". – Joseph Oct 17 '17 at 11:07label.fieldName = champNom1 The label, in this case it is the length of each segment of an electric line, appears correctly.
- With the concat expression, there is no error after the code compilation but there is no label on the line segments.
– ennine Oct 17 '17 at 18:39