0

Categorized Symbols

Hello, I am wondering if there is a way in PyQGIS to automate the process of selecting/deselecting the check marks for each categorized symbol in the included picture. I am writing a plugin and would like to be able to have the option of viewing one of the symbols at a time instead of seeing all of them at once. Thanks

  • It depends on how are you going to switch among those categories. Do you have the value/label and would like to leave only that value's category checked? – Germán Carrillo Jul 1 at 17:49
  • Yes that is correct. – sam c Jul 1 at 17:54
2

This code snippet should do the work. The key is to use updateCategoryRenderState(), which should be True for checked values, and False for unchecked ones.

layer = iface.activeLayer()
renderer = layer.renderer()

value = "100001"
for i, cat in enumerate(renderer.categories()):
    renderer.updateCategoryRenderState(i, cat.value() == value)

layer.setRenderer(renderer)
iface.layerTreeView().refreshLayerSymbology(layer.id())
layer.triggerRepaint()
| improve this answer | |

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.