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

Once created a layer, how can I hide/show it? I can enable/disable rendering of a specific layer by selecting the checkbox through QGIS, but I need to do it programmatically from python code.

How can I show/hide(not remove) a label from python code?

I'm looking for something like:

aLayer = self.addVectorLayer(uri.uri(), layerName, self.dbConn.getProviderName())
share|improve this question
I'm glad you changed the variable name vl (from the similar code sample in an earlier question) to aLayer. It's easy to confuse the lowercase letter l with the digit 1. – andytilia Dec 3 '12 at 16:15
@andytilia: you are right. I edited the old questions too. – Heisenbug Dec 3 '12 at 16:19
up vote 4 down vote accepted

You can control the layer visibility through the legend object. Here it is, using your sample code above:

aLayer = self.addVectorLayer(uri.uri(), layerName, self.dbConn.getProviderName())
legend = self.legendInteface()  # access the legend
legend.setLayerVisible(aLayer, False)  # hide the layer
# do something else
legend.setLayerVisible(aLayer, True)  # show the layer

# maybe later I want to check if the layer is visible
print legend.isLayerVisible(aLayer)

Here's the documentation for legendInterface:

Good luck!

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.