I am working on a QGIS Python plugin which includes automated map production. I use a series of map templates, each of which has a main map, an overview map (with a layer set different to the main map), and a legend which is to be linked to the main map.

In QGIS 2.2 and 2.4 this is very easy to achieve:

First load the main map layers into the canvas then:


However in QGIS 2.6 this does not work - no layers are listed in the legend. Also the legend properties panel shows the 'Map' property of the Legend as 'None' rather than 'Map 0' (this was also the case in QGIS 2.2 and 2.4). I have tried several methods to get the legend to show the correct set of layers, all of which have failed. My most recent approach was to load all layers for both maps into the map canvas, and then try to remove the overview map layer names and symbology from the legend:

model = legend.modelV2()
for r in range(0, model.rowCount()):
    for c in range(0, model.columnCount()):
        if model.index(r, c).data() == "Locality Map Layers":
            model.removeRows(r, 1)


This removes the Locality Map Layers group from the legend, but it also removes them from the main QGIS map canvas, which I do not want to do. It seems that the map canvas and the legend are both using the same model.

Is there another way to use pyqgis to remove a layer from the composer legend without also removing it from the map canvas?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.