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 basically 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)
ml.setVisible(False)
lyr2 = QgsRasterLayer(os.getcwd() + '/data/rasters/chm/chm.vrt', 'Chm')
QgsMapLayerRegistry.instance().addMapLayer(lyr2, False)
ml2 = QgsMapCanvasLayer(lyr2)
ml2.setVisible(True)
When i want to toggle their visibility i do:
ml.setVisible(True)
ml2.setVisible(False)
But unfortunately, the rendering on the map canvas stays the same.
What am i missing?
Answer
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.
No comments:
Post a Comment