3

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

rule

So each of interstate, divided, secondary is different rule added using QgsRuleBasedRendererV2

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()

but child.children is returning empty list instead of those rules.

type of Road Network is qgis._core.QgsLayerTreeLayer

2

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(layer.id())
  • I tried to do this but this silently disable the rule and the checkbox status does not changes – Nikhil Murarka Oct 6 '15 at 12:34
  • This is only a repainting issue. Fix added to the answer – Matthias Kuhn Oct 6 '15 at 12:44

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.