Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 6 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
legend.isLayerVisible(loadedLayer)
# 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

 
discard

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.