Enable and disable styling rules
I have a vector layer in QGIS. I have added a bunch of rule for this layer. This is the
QDockWidgetfor layer in QGIS main window.
So each of interstate, divided, secondary is different rule added using
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()
child.childrenis returning empty list instead of those rules.
Type of Road Network is
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 ) rules.setCheckState(False) # Synchronize the legend with the symbology iface.layerTreeView().refreshLayerSymbology(layer.id())
Accepted answer works in QGIS 2.0.
For 3.0, you must invoke setActive on the rule
rules = layer.render().rootRule().children() rules.setActive(False) iface.layerTreeView().refreshLayerSymbology(layer.id())