When I go to layer --> properties --> Legend, I can add text on symbols from an expression like round($length , 5).

After reading the document here https://qgis.org/pyqgis/3.0/core/Legend/QgsLegendSymbolItem.html#qgis.core.QgsLegendSymbolItem.setSymbol, I tried to code it as below.

symbols = QgsSymbol.defaultSymbol(line_layer.geometryType())
QgsLegendSymbolItem(symbols, "$length").setSymbol(symbols)

and it gives me error like

TypeError: QgsLegendSymbolItem(): arguments did not match any overloaded call: overload 1: too many arguments overload 2: not enough arguments overload 3: argument 1 has unexpected type 'QgsLineSymbol'

I want to do this using PyQGIS but I am having no luck, My layer is categorized into features by feature id.

I am an intern and fairly new to PyQGIS.

I want it to have like this enter image description here



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.