I want to label my vector layer by feature id, When I am trying to do it manually, it works fine but when I write the code below, it stops working and I am unable to label them by feature ID $id.
Code :
pal_layer = QgsPalLayerSettings()
text_format = QgsTextFormat()
text_format.setFont(QFont("Arial", 12))
text_format.setSize(12)
pal_layer.setFormat(text_format)
pal_layer.fieldName = "$id"
pal_layer.enabled = True
pal_layer.placement = QgsPalLayerSettings.Line
labels = QgsVectorLayerSimpleLabeling(pal_layer)
layer.setLabeling(labels)
layer.setLabelsEnabled(True)
layer.triggerRepaint()
If I use any other attribute from the table, this code works fine, but my requirement is labeling them by feature id $id, this works fine when I manually type $id in value, but using code I am having no luck.