2

I've tried 2 approaches to control what shows up in print legends. Neither of them work which is troubling because other than pruning the layerRootTree in advance of composer.addItem(legend), there seems to be no way to display a layer in print without also displaying its symbol and name (as an entry) in the print legend's output.

    # APPROACH 1 - QgsComposerLegend(c).model()
    composerLegend = QgsComposerLegend(c)
    names = []
    excludes = ['100m', '10m', 'w001001']
    layer_set = []
    for node in self.getTreeLayers():
        layer = node.layer()
        if not layer.name() in excludes:
            layer_set.append(layer.id())
            names.append(layer.name())
    r.setLayerSet(layer_set)
    iface.mapCanvas().refresh()
    composerLegend.model().setLayerSet(layer_set)

    # APPROACH 2 - QgsComposerLegend(c).modelV2()
    composerLegend = QgsComposerLegend(c)
    composerLegend.modelV2().setRootGroup(QgsProject.instance().layerTreeRoot().findGroup('Land Uses'))
    composerLegend.synchronizeWithModel()
    composerLegend.updateLegend()
    c.addItem(composerLegend)

In the second approach the subgroups don't render properly in the legend and in the first all the layers show up regardless of the constraint assumably provided by setLayerSet.

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.