I have developed a custom qgis plugin. I have a button to export the map canvas in the form of image along with the legend. I came up with the following code for the legend:
legend = QgsComposerLegend(c)
legend.model().setLayerSet(mapRenderer.layerSet())
c.addItem(legend)
The problem is that it adds all the features in the map legend whereas I only want to include the selected features in the map legend. I also tried:
legend.setLegendFilterByMapEnabled(True)
But this does not seem to work. Any ideas on how to solve this issue?