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.

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())
aLayer.Hide()
....
aLayer.Show()
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
add comment

1 Answer

up vote 2 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: http://qgis.org/api/classQgsLegendInterface.html

Good luck!

share|improve this answer
add comment

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.