Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I toggle layer visibility in QGIS via Python API?

share|improve this question
up vote 7 down vote accepted

This is from the QGIS docs, if you are running QGIS => 1.5

from PyQt4 import QtCore, QtGui
from qgis import core, gui
i = qgis.utils.iface
# load a georeferenced raster layer
loadedLayer = i.addRasterLayer('c:\\data\\a_map.png')
# get legend
legend = i.legendInterface()
# check current visibility
# set visibility off 
legend.setLayerVisible(loadedLayer, False)
# and on again!
legend.setLayerVisible(loadedLayer, True) 

I guess you would just swap out loadedLayer = i.addRasterLayer('c:\\data\\a_map.png') with the layer that you want to hide, which you could get using something like:

QgsMapLayerRegistry.instance().mapLayer(QString theLayerId)
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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