I want to automate a periodic refresh of the QGIS map canvas with updated shapefiles. A Python plugin or console script does not allow the map to update until the script terminates. Is there a way to force a map refresh in a running plugin or script, or is a stand-alone app required?
Using v1.7.4 now but could migrate to newer version.
1 Answer 1
You can use the python threading library to run a function periodically:
I have used the following statements to remove a set of graphics after 3 seconds:
from threading import Timer
Timer( 3, self._clearGraphicLayer, ()).start()
def _clearGraphicLayer(self):
for graphic in self.graphicsLayer:
self.iface.mapCanvas().scene().removeItem(graphic)
self.iface.mapCanvas().refresh()
self.graphicsLayer = []
And if needed you can have the callback function restart the timer if a certain condition is not met. But look out if you use this module wrong you can crash the entire application.
-
You may want to consider to update to QGIS 2.4 which has improved considerably concerning thread-safety when iterating layer data.Matthias Kuhn– Matthias Kuhn2014年08月20日 10:46:40 +00:00Commented Aug 20, 2014 at 10:46