4

I often have to load a lot of rasters at once into QGIS. Unfortunately, most of them appear with a "Legend" wich shows their bands (or palette for single band rasters). This takes a lot of space and makes it much more difficult to handle my layers.

enter image description here

Is there a way to hide this legend by default when adding new rasters? Or at least is it possible to "hide legend for all selected layers" so i do not have to click on the small arrow to the left of the name of each layer?

Taras
  • 32,823
  • 4
  • 66
  • 137
sn1ks
  • 2,982
  • 12
  • 28
  • 1
    I have a few only partial and/or clumsy workarounds. You could use the collapse all button, but it does affect all layers. If you have an empty QGIS project open you could add the rasters to it and use the collapse all button there, then copy and paste the collapsed layers into your working project. Or you could group all your newly added rasters and then collapse the group if it doesn't do so automatically. But this hides the individual rasters and if you expand the group later then they each appear expanded. – John Nov 03 '21 at 13:38

2 Answers2

3

Let's assume there are some rasters, that were already added and can be viewed in the Layer Panel, see image below

input

To hide rasters' bands or palettes, proceed with Plugins > Python Console > Show Editor (Ctrl+Alt+P) and use the script below

# accessing map layers
layers = QgsProject.instance().mapLayers()
# accessing layers' tree root
root = QgsProject.instance().layerTreeRoot()

iterating over layers

for layer in layers.values(): # filtering only raster layers if layer.type() == QgsMapLayerType.RasterLayer: LayerNode = root.findLayer(layer.id()) LayerNode.setExpanded(False) # hidding the bands etc.

Press Run script run script and get the output like this

result


References:

Taras
  • 32,823
  • 4
  • 66
  • 137
  • this does the trick thank you! but does this really have to be done with a python script? do you have any idea why only some of the 3 band - rasters come with expanded legend state and some do not? – sn1ks Nov 03 '21 at 11:06
  • Saying honestly I do not know why only some of the 3 band - rasters come with expanded legend state and some do not. Maybe it deserves asking as a new question. – Taras Nov 03 '21 at 11:50
2

You know there is a "Collapse All"? But that will obviously collapse all, not only the ones you want...

enter image description here

BERA
  • 72,339
  • 13
  • 72
  • 161