16

Is it possible to have a transparency slider shown by default in the Layer window, i.e. directly after adding a new layer, instead of having it to enable for each layer manually via layer-properties?

Taras
  • 32,823
  • 4
  • 66
  • 137
Matty
  • 183
  • 5

2 Answers2

14

This was briefly mentioned in QGIS - Developer forum where you need to use QgsMapLayer::setCustomPropertylayer to enable the embedded widget for your layer. We can then add an itemAdded event so that whenever a layer is added, it will automatically be shown with the transparency widget.

So you could use something like the following in the Python Console:

Updated solution for QGIS 3.x

def transparency_slider(layers):
    for layer in QgsProject.instance().mapLayers().values():
        if layer.customProperty("embeddedWidgets/count") != 1 or layer.customProperty("embeddedWidgets/0/id") != 'transparency':
            layer.setCustomProperty("embeddedWidgets/count", 1)
            layer.setCustomProperty("embeddedWidgets/0/id", "transparency")
        else:
            pass
        qgis.utils.iface.layerTreeView().refreshLayerSymbology(layer.id())

QgsProject.instance().layersAdded.connect(transparency_slider)

Old recipe for QGIS 2.18.3 (tested on Win7 64-bit.)

def transparency_slider():
    for layer in QgsMapLayerRegistry.instance().mapLayers().values():
        if layer.customProperty("embeddedWidgets/count") != 1 or layer.customProperty("embeddedWidgets/0/id") != u'transparency':
            layer.setCustomProperty("embeddedWidgets/count", 1)
            layer.setCustomProperty("embeddedWidgets/0/id", "transparency")         
        else:
            pass
        qgis.utils.iface.legendInterface().refreshLayerSymbology(layer)

Connect "itemAdded" event to "transparency_slider" function

legend = qgis.utils.iface.legendInterface() legend.itemAdded.connect(transparency_slider)


Example:

Inserting code into python console and before adding shapefiles:

Before

Result:

After

ThomasG77
  • 30,725
  • 1
  • 53
  • 93
Joseph
  • 75,746
  • 7
  • 171
  • 282
  • That's fantastic! How do you then turn that off if you want to bring in subsequent vector layers without the transparency slider? – Martin Hügi Mar 17 '17 at 12:29
  • 1
    @MartinHügi - You can just type legend.itemAdded.disconnect(transparency_slider) in the python console ;) – Joseph Mar 17 '17 at 12:35
  • @MartinHügi - Most welcome, glad it does =) – Joseph Mar 17 '17 at 12:37
  • 1
    Excellent script! Is there a way to keep this even when restarting QGIS or do you have to perform the script on every restart? – blabbath Mar 19 '17 at 14:43
  • 1
    @poellinf - As described in this post: Is there a way to run a python script on opening a QGIS project?, you can make either a startup.py script (which runs the script everytime QGIS starts up) or make a project macro (which runs only on your project). Using either method, you will need to add import qgis; from qgis.core import QgsMapLayerRegistry in your script :) – Joseph Mar 20 '17 at 10:30
  • On a personal PC I have QGIS 2.16.3 and when I run this script in an existing project it applies the transparency slider to new and existing layers. So it seems that it doesn't have to be run before bringing layers in. However I have tried the exact same thing on a work PC running QGIS 2.14.9 and nothing happens? No transparency sliders are applied. Is this because it is QGIS 2.14, or is it anything to do with the fact that I don't have admin right on my work PC, so I can alter the program files?? – Martin Hügi Mar 20 '17 at 18:12
  • @MartinHügi - You're exactly right, the script iterates through all layers in the table of contents and applies the transparency widget if it is not already enabled. These sliders were introduced in QGIS 2.16 which would explain why nothing happens in 2.14 ;) – Joseph Mar 21 '17 at 09:51
  • Thanks, that would explain it. I'll wait for the next LTR and then ask for our work laptops to be upgraded. – Martin Hügi Mar 21 '17 at 10:11
6

There is a plugin called Raster Transparency, that as the name suggests, will open a dockable panel with sliders for changing the transparency of a selected raster layer.

Go to Plugins > Manage and install plugins - Find it and install, a new associated icon will appear on your toolbars.

Martin Hügi
  • 3,612
  • 2
  • 22
  • 51