<div dir="ltr"><div>Hi All,<br><br></div>QGIS 2.6 has surely a lot of improvement on composer, but I find myself stuck on a simple problem on composer legend manipulation. Instead of showing from all the layers, I want to just show a specific layer. This is a simple code that you can run in QGIS Python Console:<br><br><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote">from qgis.core import QgsComposition, QgsLayerTreeGroup<br>from PyQt4 import QtXml, QtCore<br><br>canvas = iface.mapCanvas()<br>renderer = canvas.mapRenderer()<br>composition = QgsComposition(renderer)<br><br>composer = iface.createNewComposer()<br>composer.setComposition(composition)<br><br>template_file = QtCore.QFile('/tmp/inasafe-portrait-a4.qpt')<br>template_file.open(QtCore.QIODevice.ReadOnly | QtCore.QIODevice.Text)<br>template_content = template_file.readAll()<br>template_file.close()<br><br>document = QtXml.QDomDocument()<br>document.setContent(template_content)<br><br>status_load = composition.loadFromTemplate(document)<br><br>legend = composition.getComposerItemById('impact-legend')<br>legend.setTitle('Hoho')<br><br>group = QgsLayerTreeGroup()<br>group.addLayer(iface.activeLayer())<br><br># 1 Somehow crashes QGIS in inasafe, but not in qgis console<br>#legend.modelV2().setRootGroup(group)<br>#legend.synchronizeWithModel()<br><br># 2 No effect on legend, sigh<br>#legend.model().setLayerSetAndGroups(group)<br>#legend.synchronizeWithModel()<br><br># 3 No effect on legend, sigh. Using QgsLegendModel basically obsolete in 2.6, not deprecated<br>#legend.model().setLayerSet(iface.activeLayer().id())<br>#legend.synchronizeWithModel()<br><br># 4 Unfortunately setCustomLayerTree is private method<br>#legend.setCustomLayerTree(group) <br>#legend.synchronizeWithModel()<br>#<br><br># 5 legendmodel linked directly to layer registry in map canvas, so this will delete layer in canvas<br>#legend.updateLegend()<br>#model = legend.modelV2()<br>#for r in range(0, model.rowCount()):<br>#    for c in range(0, model.columnCount()):<br>#        if model.index(r, c).data() != iface.activeLayer().name():<br>#            model.removeRows(r, 1)<br>#legend.synchronizeWithModel()<br><br>composition.exportAsPDF('/tmp/result.pdf')<br clear="all"></blockquote><div><div><br></div><div>I have tried several possibilities to do that by going through the c++ code itself. The 1st possibility above is working fine actually in python console, but somehow it crashes QGIS implementing it in the plugin. The 2nd and 3rd are giving no effect (It says the QgsLegendModel deprecated, but if it's not working, shouldn't it say obsolete?). The method setCustomlayerTree in the 4th unfortunately not available in the SIP. The 5th will delete layer in map canvas.<br><br></div><div>>From those 5 possibilities, is there any other way to do this simple task?<br><br></div><div>Regards<br></div><div>-- <br><div class="gmail_signature"><div dir="ltr"><div><p><b><span style="font-size:11pt;font-family:"Calibri","sans-serif";color:rgb(31,73,125)">-------------------</span></b></p><b>Akbar Gumbira<br></b></div><b>Software Engineer</b><br><b>Geospatial, NLP, Data Mining, Machine Learning, Artificial Intelligence</b></div></div>
</div></div></div>