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())
  • 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, 2012 at 16:15
  • @andytilia: you are right. I edited the old questions too.
    – Heisenbug
    Dec 3, 2012 at 16:19

1 Answer 1


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.legendInterface()  # 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: http://qgis.org/api/classQgsLegendInterface.html

Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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