I would like to set visibility of a layer group and all of its children (and grand-children and so on...) using PyQGIS (QGIS 2.14.1).

Assume the groups name is g0 with subgroups g0[1...n] and subsub...groups g0[1...n][1...n][...].


sets the group and ALL what comes beneath invisible, so far so good:

enter image description here

But vice versa


sets the group visible and leaves all its children untouched, wich results in nothing visible at all:

enter image description here


only touches the direct children and not recursively all the descendants:

enter image description here

I wonder why manually setting the root groups (g0) visibility does result in recursively setting the visibility of all beneath it at all.

Question: Do I have to implement some recursive function to set the visibility of a group and its whole big family, or is there a more convenient way to accomplish this? Or is here something buggy?

  • 2
    Just to mention I fixed the word visibility 7 times in your question. :D Apr 13 '16 at 19:24

It's not a bug. It's a feature. :D

Check boxes in the layer tree have three states (see Qt docs):

Qt::Unchecked            0  
Qt::PartiallyChecked     1 (some children are checked and some are not) 
Qt::Checked              2

Therefore, the setVisible() function expects a Qt.checkState value, not a boolean value. By chance, it seems that False is resolved to 0 and True to 1. The value 2 is what you're actually looking for:


Or, if you prefer:

from PyQt4.QtCore import Qt

This call will check the g0 group and all its children (grandchildren, great grandchildren, and so on).

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.