[GIS] Can QGIS map canvas update while python script is running

pyqgis

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.

Best Answer

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.

More info: https://docs.python.org/2/library/threading.html

Related Question