I am using a print composer from QGIS using python. Inside a QGIS project (
.qgz, QGIS version 3.18.3), I created a print composer layout called "mylayout" and added a map and a legend. I would like to apply a
QgsExpression with python to the "Soil cover" item of the legend (see image).
Expression I want to apply (Automatic wrap text (line break) in map composer legend)
Current legend (left collapsed, right expanded)
I created a
QgsExpression and a
QgsExpressionContext linked to the legend but I have no clue of what to do next.
# sample of init code manager = project_instance.layoutManager() # get layout manager from Qgis project layout = manager.layoutByName("mylayout") # get existing layout in layout manager # define expression and expression context legend = layout.itemById("legend") # get legend from layout legend.setAutoUpdateModel(False) # not sure about this one exp = QgsExpression("wordwrap(@symbol_label, 30)") context = legend.createExpressionContext() # I tried exp.evaluate(context)