Skip to main content
3 of 8
added 2 characters in body
xunilk
  • 30.2k
  • 4
  • 43
  • 82

The problem is that 'QgsVectorLayer' objects have not attribute 'renderer'. This kind of method is for raster layers. If you want to change the transparency of QgsVectorLayer objects you have to use the method: 'setLayerTransparency(int)'; located in QgsVectorLayer class.

Next code works for these kind of objects:

mc=iface.mapCanvas()
 
layers=[]

n = mc.layerCount()

for i in range(n):
    layers.append(mc.layer(i))

for layer in layers:
    layer.setLayerTransparency(90)
    layer.triggerRepaint()

I tested it in QGIS. See next image:

enter image description here

You can modify the above code for including QgsRasterLayer objects too.

xunilk
  • 30.2k
  • 4
  • 43
  • 82