Friday, 27 December 2019

pyqgis - Checking/unchecking rule-visibility of layers at once in QGIS


I've got several rule-based layers in my project (qgis 2.18.13). Is it possible to check/uncheck the visibility of all rules (not the visibility of the layer itself) at once? If yes - is it possible to do this on all layers of the project at once too?



Answer



You could use something like the following to check or uncheck all rule-based style categories from all layers loaded in your project:



from PyQt4.QtCore import Qt

for layer in QgsMapLayerRegistry.instance().mapLayers().values():
renderer = layer.rendererV2()
if renderer.type() == 'RuleRenderer':
ltl = QgsProject.instance().layerTreeRoot().findLayer(layer.id())
ltm = iface.layerTreeView().model()
legendNodes = ltm.layerLegendNodes(ltl)
for ln in legendNodes:
ln.setData(Qt.Unchecked, Qt.CheckStateRole)

#ln.setData(Qt.Checked, Qt.CheckStateRole)

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...