mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-07 00:05:28 -04:00
called when the task has completed (successfully or otherwise). This allows for simpler task design when the signal/slot based approach is not required. Just implement run() with your heavy lifting, and finished() to do whatever follow up stuff should happen after the task is complete. finished is always called from the main thread, so it's safe to do GUI operations here. Python based tasks using the simplified QgsTask.fromFunction approach can now set a on_finished argument to a function to call when the task is complete. eg: def calculate(task): # pretend this is some complex maths and stuff we want # to run in the background return 5*6 def calculation_finished(result, value=None): if result == QgsTask.ResultSuccess: iface.messageBar().pushMessage( 'the magic number is {}'.format(value)) elif result == QgsTask.ResultFail: iface.messageBar().pushMessage( 'couldn\'t work it out, sorry') task = QgsTask.fromFunction('my task', calculate, on_finished=calculation_finished) QgsTaskManager.instance().addTask(task) Multiple values can also be returned, eg: def calculate(task): return (4, 8, 15) def calculation_finished(result, count=None, max=None, sum=None): # here: # count = 4 # max = 8 # sum = 15 task = QgsTask.fromFunction('my task', calculate, on_finished=calculation_finished) QgsTaskManager.instance().addTask(task)