[Qgis-user] Help with cached join layer refresh using PyQGIS

Kory Roberts kaptainkory at gmail.com
Wed Jan 31 19:50:41 PST 2018


For reasons I won't go too deeply into, unless requested, I regularly 
need to run a very short PyQGIS query/filter code on 2 Spatialite layers:

query = '"id_a" IN (47, 120)'
canvas = qgis.utils.iface.mapCanvas()
allLayers = canvas.layers()
for layer in allLayers:
     if layer.name() == 'Layer_A':
         layer.setSubsetString(query)
     elif layer.name() == 'Layer_B':
         layer.setSubsetString(query)

Layer_A contains a geometry field, Layer_B does not.  Layer_B is a view, 
which honestly is very slow to process.  I have these joined in QGIS: 
Layer Properties -> Joins.  For performance reasons, I have "Cache join 
layer in virtual memory" selected, otherwise editing is *unbearably 
slow* to the point that QGIS completely locks up for long periods of time.

The issue then, is that after I run the code subsequent times (changing 
for new queries), I have to go back into Layer Properties -> Joins -> 
specific join, click OK (without need to change any settings) to refresh 
the cache.

Probably a long way to this, but...  Is there a way in PyQGIS to refresh 
the cache for the join layer and just add that line in at the end of the 
already existing code?

Thanks.




More information about the Qgis-user mailing list