[GIS] How to set the transparency for multiple layers or add a global transparancy preference

qgistransparency

How can I set the transparency to 27 % for all 245 layers in QGIS? The only way I know of is by right clicking on the layer name, selecting Properties, then Transparency and then moving the transparency slider left or right.

a

This is simple enough. But this is only good for up to 10 layers maybe. What if you have 245 layers like I do? Do you just keep on repeating the process? Now surely, there there must be a way to apply this to all 245 layers at once!?

b

Alternatively, is there a global transparency preference setting I can add in so that when I add new layers they automatically get 27 % transparency?

The QGIS online documentation mentions something about exporting your transparency setting to a file for a later use.

As you can see this is quite easy to set custom transparency, but it
can be quite a lot of work. Therefore you can use the button Export to
file to save your transparency list to a file. The button Import from
file loads your transparency settings and applies them to the current
raster layer.

This seems like a useful feature. But I don't think this is what I'm looking for.

I tried selecting multiple layers in the table of contents and then right click and select Properties and set the transparency level, apply changes and click OK. It applied the changes, but only to the last layer in the selection, the one I right clicked on. None of the other layers in the selection were affected. (This could be a bug actually.)

Best Answer

A little python script like this would do it

for layer in iface.legendInterface().layers():
   layer.renderer().setOpacity(0.2)

Just copy and paste that into the python console (which is shown via Plugins->Python console) and hit enter twice. The script will loop through all layers and set their opacity to 20%.

For QGIS 3.x you need a slightly different Python script because the API has changed significantly:

layers = qgis.core.QgsProject.instance().layerTreeRoot().layerOrder()
for layer in layers:
    layer.setOpacity(0.2)
# Redraw the layers so you can see the effect
iface.mapCanvas().refreshAllLayers()