In continuity in my question How to add a legend to a canvas in a standalone PyQGIS application? > In every addition of a new layer in my standalone application, the legend lengthens and becomes overloaded. So I would like to know how to remove one or several layers with his(their) legend(s). Sorry for my english.

closed as off-topic by PolyGeo Mar 16 '18 at 8:28

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking help to debug/write/improve code must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Providing a clear problem statement and evidence of a code attempt will help others to help you. See: How to create a Minimal, Complete, and Verifiable example." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

  • I haven't been able to remove layers safely using QgsMapLayerRegistry.instance().removeMapLayer(id). I keep getting segmentation faults when removing/adding layers (like here: osgeo-org.1560.x6.nabble.com/…). Could you confirm it? – Germán Carrillo Apr 14 '15 at 12:59

your question isn't a QGIS question, but a QT question. Legend tree is managed by QgsLegendTreeView that is a QTreeView. So all what you need is to browse you tree an hide rows to compact your legend with http://doc.qt.io/qt-4.8/qtreeview.html#setRowHidden If you simply want to remove a layer from QGIS (standalone or not) simply unregister it from QgsMapLayerRegistry with QgsMapLayerRegistry.instance().removeMapLayer(layerId)

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