Hi all
I’m having lots of trouble with the composer legend in 2.6. I am working on a python plugin which includes automated map production. These automated maps are based on templates which have a main map, a legend linked (within the .qpt file) to this main map, and an overview map with a set of layers different to those for the main map.
The relevant part of the workflow, which works as expected in QGIS 2.2 and 2.4, runs like this:
I have tried the following:
1. In QGIS 2.2 and 2.4 the following two lines were sufficient to ensure that the legend showed the correct layers :
mainMap.storeCurrentLayerSet()
mainMap.setKeepLayerSet(True)
However in 2.6, the legend remains empty when these lines are used. In 2.6 I have tried:
2. 2. Use AutoUpdateModel:
legend.setAutoUpdateModel(True)
legend.updateLegend()
legend.setAutoUpdateModel(False)
However this loads all layers into the legend, including the overview layers (which have their visibility set to zero) even if the layer set for the main map has been locked.
3. 3. Use setLegendFilterByMapEnabled(True): my thinking was that since the overview layers are not visible, they would be excluded from the legend.
Inserting this line after line 2 in scenario 1, or before line 1 in scenario 2, makes no discernible difference – the results are the same as in 1) and 2) above (depending on whether I use setAutoUpdateModel(True)).
4. 4. Allow all layers to be listed in legend, but then try to remove the overview layers from the legend as follows:
model = legend.modelV2()
for r in range(0, model.rowCount()):
for c in range(0, model.columnCount()):
if model.index(r, c).data() == "Overview Map Layers":
model.removeRows(r, 1)
legend.updateLegend()
This removes the Overview 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 mucks up any other composers I have created for that project using the MPT). It seems that the map canvas and the legend are both using the same model.
Can anyone suggest how in QGIS 2.6 I can use pyqgis to ensure a composer legend contains only a specific subset of the full set of layers from the canvas?
Thanks
Pat