13

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?

11

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:

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)

Tested on QGIS 2.18.3 for Win7 64-bit.


Example:

Inserting code into python console and before adding shapefiles:

Before

Result:

After

  • 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
  • @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
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.