I'm trying to get layers to update automatically when their data source changes. I'm using R to write a shapefile with an attribute, and colouring according to that attribute in QGIS.
I want to write a new shapefile with different attribute values, and have the Qgis map colours update. Step 1 is triggering that process, step 2 is making the layer reload from the modified shapefile. Its step 2 I am worrying about here.
Other questions/mailing list chatter mentions using
triggerRepaint on the layer - that doesn't work. Other suggestions include
setCacheImage(None) and again that doesn't work. The layer does update eventually, but I really can't see the logic, and sometimes it happens by surprise after I've done nothing. Or maybe I did something two minutes ago.
The one reproducible way of getting it to update is to duplicate the layer from the legend menu - the duplicate always gets its data from the current shapefile, and the original layer updates itself too! So there must be some way of doing it.
I think it was working better in 2.8, but this is 2.10 so maybe there's a new bug somewhere.
Related, but doesn't work for me in 2.10:
Other things I've tried:
layer.dataProvider().dataChanged.emit()- worked once, then not again on the same layer
I think I've tracked down why duplicating the layer works - if I create a new throwaway layer based on the updated layer and then call
.triggerRepaint() on the updated layer, it updates on the map canvas:
QgsVectorLayer( layer.source(), "layer copy", layer.providerType() )
If I use a different layer source it doesn't work, so it seems to be if you create a layer object based on the same layer source...
A quick test just now with a raster layer (from a GeoTIFF), and just calling
rlayer.triggerRepaint() seems to reliably update the view of the raster in the map canvas.