[Qgis-developer] Remove layer from layer tree without destroying the layer

Martin Dobias wonder.sk at gmail.com
Mon Aug 22 20:47:20 PDT 2016


On Mon, Aug 22, 2016 at 7:57 PM, Casper Børgesen (CABO) <CABO at niras.dk> wrote:
> I have added a layer to the layer tree and now I want to remove it from the
> layer tree:
>
> self.layer = self.create_my_layer()
> QgsProject.instance().layerTreeRoot().insertLayer(0, self.layer)
> QgsProject.instance().layerTreeRoot().removeLayer(self.layer)
>
> But it seems like my layer has been destroyed while removing it from the
> layer tree. Is it possible to not destroy the layer while still removing it
> from the tree?

It is possible - just temporarily disable the bridge to map layer
registry before removing the layer:

QgsProject.instance().layerTreeRegistryBridge().setEnabled(False)

... and then enable it again.

You should make sure to remove the layer from the map layer registry
later when you have finished using it. Otherwise if user saves the
project, that layer will still be referenced (and loaded when the
project loads) but it will not be accessible from the GUI.

Cheers
Martin


More information about the Qgis-developer mailing list