1

If I use PyQGIS to delete style categories of a layer the feature count gets messed up:

Before deleting the category (style class) "3x20/15mm /VL" the featurecount shows 1 feature which is correct:

enter image description here

After deleting the 2nd category the featurecount of the class "3x20/15mm /VL" is incorrect:

enter image description here

I use following PyQGIS to delete the category:

layer=iface.activeLayer()
r = layer.renderer()
layerCategories = r.categories()
print(layerCategories)
r.deleteCategory(1)
#del layerCategories[0]
if r is not None:
    layer.setRenderer(r)
layer.dataProvider().forceReload()
if iface.mapCanvas().isCachingEnabled():
    layer.triggerRepaint()
else:
    iface.mapCanvas().refresh()
mView = iface.layerTreeView()
mView.refreshLayerSymbology(layer.id())

Some test data can be found here: https://gist.github.com/thbaumann/5af83e18ba2de95fb0f65256a85e2417

Is this a QGIS3 bug or am I doing something wrong?

1
  • A clue indicating it's a bug in my opinion model = mView.layerTreeModel();print([i.data(0) for i in model.children()]) The underlying model to the view does not refresh. FYI, I've also seen that If you save and reopen the project after your PyQGIS operations, the legend is refreshing...
    – ThomasG77
    Commented Jun 27, 2020 at 14:31

1 Answer 1

2

Although it seems a bug in my opinion (see my comment), you can bypass it using the following (tested)

layer.setRenderer(layer.renderer().clone())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.