4

When a layer is added to QGIS, it is ordinarily added to "Layers" panel and "Map Canvas". The same thing happens when a memory layer (loaded/created by QgsVectorLayer) is added using QgsProject.instance().addMapLayer.

layer = QgsVectorLayer("Polygon?crs=EPSG:4326", "Buffer", "memory")
# 
QgsProject.instance().addMapLayer(layer) # after this line, layer is added to both area.

When writing a script step by step, I usually add intermediate layers to control if the script runs correctly. Then I need to remove them manually and repeatedly.

I want a layer which I add to memory to be added to "Map Canvas" only. How can I achieve that? Is it possible to break the connection between "Layers" panel and "Map Canvas".

For example, as shown in the image, I want Buffer layer to appear only in "Map Canvas", but not in "Layers" panel.

enter image description here

Kadir Şahbaz
  • 76,800
  • 56
  • 247
  • 389
  • 3
    Have a look at: https://gis.stackexchange.com/questions/229826/how-to-make-a-layer-or-group-invisible-in-the-layers-panel/230804#230804 I even have proposed this functionality to QGIS Core, it's still in an early stage, but gaining more attention and receiving valuable feedback: https://github.com/qgis/QGIS-Enhancement-Proposals/issues/155 – Germán Carrillo Mar 06 '20 at 21:13
  • I just realized that this question is not duplicate of the other question. When you apply the answer under the other question, the layer stays in the project, but it disappears from the layer list and from the canvas. I would like the layer to remain in the canvas. – Kadir Şahbaz Dec 21 '22 at 11:32
  • Ok, it was duplicate. When I used it before, I got a different result. – Kadir Şahbaz Dec 21 '22 at 11:55

1 Answers1

4

I‘ve had the same question for a while and didn't find a proper solution. Thanks to @Germán Carrillo's post and plugin, I found a way. Thank you so much.

Sorry I cannot install an extra plugin at work, so I summarised some snippets from Germán's scripts. It works well for me, hopefully it could be useful to others as well.

If you are interested in the code, please read Germán's link above or go to the plugin github page.

# get the current layer or use a temp layer as @Kadir did above
layer = iface.activeLayer()

ltv = iface.layerTreeView() model = ltv.model() root = QgsProject.instance().layerTreeRoot()

index = model.node2index( root.findLayer(layer.id()))

use True to hide the layer

ltv.setRowHidden( index.row(), index.parent(), True ) ltv.setCurrentIndex( model.node2index( root ) )

You can set the layer to be visible again by

ltv.setRowHidden( index.row(), index.parent(), False )

It can also be remove when it's invisible

QgsProject.instance().removeMapLayer(layer.id())
iface.mapCanvas().refresh()

UPDATE:

Recently, I found QGIS documentation provided a solution to this question and it's almost the same as shown above. Take a look if you like. Go to this link, and search "Hidden node trick" on the page.

Zac
  • 517
  • 3
  • 10
  • I saw the snippset but it uses the iface wich is not available in a standalone script. I couldn't be able to hide a node without it. – SIGIS Jun 25 '21 at 10:49