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?
2 Answers
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:
Result:
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.
- 3,612
- 2
- 22
- 51


legend.itemAdded.disconnect(transparency_slider)in the python console ;) – Joseph Mar 17 '17 at 12:35import qgis;from qgis.core import QgsMapLayerRegistryin your script :) – Joseph Mar 20 '17 at 10:30