Enable and disable styling rules

Enable and disable styling rules

I have a vector layer in QGIS. I have added a bunch of rule for this layer. This is theQDockWidgetfor layer in QGIS main window.

So each of interstate, divided, secondary is different rule added usingQgsRuleBasedRendererV2.

All the rule have the check box where I can manually toggle the visibility of the layer. Is there any way I can toggle this using python?

I tried to use layer tree

root = QgsProject.instance().layerTreeRoot() for child in root.children(): print type(child), child.layerName(), child.children()

butchild.childrenis returning empty list instead of those rules.

Type of Road Network isqgis._core.QgsLayerTreeLayer.

This is a property of the layer, not of the layer tree.

Get the layer, and modify its renderer's rules

# A list of the layers' rules rules = layer.rendererV2().rootRule().children() # Deactivate a rule (in this example the first rule [0]) rules[0].setCheckState(False) # Synchronize the legend with the symbology iface.layerTreeView().refreshLayerSymbology(

Accepted answer works in QGIS 2.0.

For 3.0, you must invoke setActive on the rule

rules = layer.render().rootRule().children() rules[0].setActive(False) iface.layerTreeView().refreshLayerSymbology(