2

When programmatically applying a subset string to a vector layer (here: ax_flurstueck with object count 34265)

enter image description here

via

l = iface.activeLayer()
l.setSubsetString('gemarkungsnummer in ('2760','2765','2771','2780','2786','2800','2811','2817','2836')')

the map canvas refreshes automatically, but not the object count in the layer tree (displays still 34265 objects):

enter image description here

When moving the main window around or changing the width of the layer tree or something like that, the layer tree refreshes (21581 objects).

enter image description here

The question is, how to achieve this programmatically? I did not find something like:

QgsLayerTreeView.refesh()

in analogy to refreshing the map canvas. Am I missing something?

4

This is a quick response but this code work perfectly,and you can refresh featurecount on QgsLayerTreeView.

1:Option

l = iface.activeLayer()
l.setSubsetString('gemarkungsnummer in ('2760','2765','2771','2780','2786','2800','2811','2817','2836')')
qgisTView = qgis.utils.iface.layerTreeView()
actions=qgisTView.defaultActions ()
actions.showFeatureCount ()
actions.showFeatureCount ()

2: Option

Replicate core code in Python.

iface=qgis.utils.iface
l = iface.activeLayer()
l.setSubsetString('gemarkungsnummer in ('2760','2765','2771','2780','2786','2800','2811','2817','2836')')

mView= iface.layerTreeView()
node = iface.layerTreeView().currentNode()
nodeslist=mView.selectedLayerNodes()
newValue = node.customProperty("showFeatureCount", 0 ) 
for value in nodeslist:
    value.setCustomProperty("showFeatureCount", int(newValue));
  • Great job +1, but just one question (or two respectivly): 1. How did you figure out that actions.showFeatureCount() is required twice and 2. Why is that so? I tested this with only one actions.showFeatureCount() and this does not work, my first thought was that this was some 'careless mistake' due to the late evening time you wrote the answer... :-) – Jochen Schwarze Jul 28 '16 at 7:04
  • Closer looked at, stupid question of mine. The first actions.showFeatureCount() uncheckes the 'show feature count' checkbox, the second checkes it again ... – Jochen Schwarze Jul 28 '16 at 7:21
  • you can replicate the core code,too: qgis.org/api/qgslayertreeviewdefaultactions_8cpp_source.html , line 160 – Fran Raga Jul 28 '16 at 8:08

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.