5

I have a QgsCategorizedSymbolRenderer and I'm trying to configure it so that symbol_a:

  1. appears at the top of the layers window legend, but also
  2. appears on top of all other QgsRendererCategory symbols (i.e. it should be rendered last).

In QGIS, I can do this by going to Symbology > Advanced > Symbol Levels, checking Enable symbol levels and configuring the Layer 0 for Label A to 1, leaving the others at 0.

Using PyQGIS, I can enable symbol levels, but can't find a way to change the specific symbol levels (i.e. the order they are rendered). Re-ordering the categories achieves this, but I'd prefer Label A to appear first in the layers window. Is this possible?

My existing code looks something like this:

categories = [
    QgsRendererCategory('value_a', symbol_a, 'Label A'),
    QgsRendererCategory('value_b', symbol_b, 'Label B'),
    QgsRendererCategory('value_c', symbol_c, 'Label C'),
]

renderer = QgsCategorizedSymbolRenderer('field_name', categories)
renderer.setUsingSymbolLevels(True)
2

1 Answer 1

1

This post give a good answer with a clause applied on the renderer :

myRenderer.setOrderBy(QgsFeatureRequest.OrderBy([QgsFeatureRequest.OrderByClause('Value', False)]))
myRenderer.setOrderByEnabled(True)

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.