So I have a function in a utility class that creates my Legend for the composer. It looks something like this:

def addLegend(self, c, r, L, tree_root):


  1. L is an QgsComposerLegend
  2. c is the QgsComposition
  3. r is the iface.mapCanvas().mapRenderer()
  4. tree_root is an arbitrary group in the tree of layers and groups originating from QgsProject.instance().layerTreeRoot()

it works but my requirements bring up these issues:

  1. The legend should display a colored box for the groups
  2. The layers should not show up in the legend even though they do in the map

closed as too broad by PolyGeo Mar 20 '18 at 6:37

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Even though they are unmarked you seem to be asking two questions which makes this too broad. – PolyGeo Mar 20 '18 at 6:37

I solved this by applying a single color to all the polygons captured by my query across layers. Then I created a legend folder into which I added stub memory polygon layer with a zero dimension polygon giving it the corresponding query's color. I then pass this group to the legend for processing.

  • Did you do this programmatically? Could you share your code? – raphael Dec 29 '16 at 2:13
  • I think that the answer to a coding question is always far better if it includes code. – PolyGeo Mar 20 '18 at 6:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.