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][…].
QgsProject.instance().layerTreeRoot().findGroup('g0').setVisible(False)
sets the group and ALL what comes beneath invisible, so far so good:
But vice versa
QgsProject.instance().layerTreeRoot().findGroup('g0').setVisible(True)
sets the group visible and leaves all its children untouched, wich results in nothing visible at all:
QgsProject.instance().layerTreeRoot().findGroup('g0').updateChildVisibility()
only touches the direct children and not recursively all the descendants:
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?
Best Answer
It's not a bug. It's a feature. :D
Check boxes in the layer tree have three states (see Qt docs):
Therefore, the
setVisible()
function expects aQt.checkState
value, not a boolean value. By chance, it seems thatFalse
is resolved to 0 andTrue
to 1. The value 2 is what you're actually looking for:Or, if you prefer:
This call will check the
g0
group and all its children (grandchildren, great grandchildren, and so on).