Im having a problem toggling the visibility of map layers in PyQGIS (2.4). I have a simple interface, just the map canvas, no legend, and a radio button that toggles between two raster layers.

Layers are initialized like this:

lyr = QgsRasterLayer(os.getcwd() + '/data/rasters/dof/dof.vrt', 'Ortophoto')
QgsMapLayerRegistry.instance().addMapLayer(lyr, False)
ml = QgsMapCanvasLayer(lyr)
lyr2 = QgsRasterLayer(os.getcwd() + '/data/rasters/chm/chm.vrt', 'Chm')        
QgsMapLayerRegistry.instance().addMapLayer(lyr2, False)
ml2 = QgsMapCanvasLayer(lyr2)

When I want to toggle their visibility I do:


But unfortunately, the rendering on the map canvas stays the same.

What am I missing?

3 Answers 3


For QGIS 3, you have to use:


or to be safe

node = QgsProject.instance().layerTreeRoot().findLayer(layer_id)
if node:

Reading the documentation, findLayer accepts both the layer itself and the layer ID as a string.

  • Is it possible to use a variable instead of the layer name in 'layer.id()' ?
    – Leo
    Commented Feb 4, 2020 at 5:41
  • 1
    yes, I update the code and added a pointer to the documentation. Commented Feb 4, 2020 at 5:53

At the moment (QGIS 2.x) (and I know it's a bit gross) but you also have to update the layer set on the canvas to update the layer state change

canvas.setLayerSet([ml2, ml])

This will also control the render order. m1 will reneder first then ml2 on top of it.

  • THIS is the correct answer. GIS.SE saves the day once more ;>
    – U2ros
    Commented Oct 22, 2014 at 13:46

In QGIS 2, You have to refresh the mapCanvas as well:



After this the map Canvas will redraw and you can see the changes.

Normally you should see changes when you zoom in and out to force a refresh of the mapCanvas manually.

To set the layer visibility you can use this:

 qgis.utils.iface.legendInterface().setLayerVisible(m1, False)
 qgis.utils.iface.legendInterface().setLayerVisible(m12, True)

I think mapCanvas doesn't have a method to set teh layer visibility. A setLayerVisibility can only be found in the QgsLegendInterface:


If this doesn't work you can also change the transparency of a raster layer with QgsRasterTransparency.TransparentSingleValuePixel() like in this post

How do I set layer transparency in QGIS 2.0 with Python?

  • Doesn't work.I tried zooming in/out anyway.
    – U2ros
    Commented Oct 22, 2014 at 12:02
  • I refreshed the map canvas instance directly self.ui.map.refresh()
    – U2ros
    Commented Oct 22, 2014 at 12:03
  • 1
    Must be something else in my code then. It is a helpful if i know this is the way to do it ;>
    – U2ros
    Commented Oct 22, 2014 at 12:04
  • Thanks for updating your answer... in 2.4 qgis.utils no longer exists. How do you access iface.legenInterface()?
    – U2ros
    Commented Oct 22, 2014 at 12:44
  • I use Qgis2.4 and i use for example to get the active layer in a plugin layer = qgis.utils.iface.activeLayer() or import qgis.utils
    – Martin
    Commented Oct 22, 2014 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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