Bug report #19267
QgsTask does not starts after adding to the task manager
|Category:||Python bindings / sipify|
|Affected QGIS version:||3.3(master)||Regression?:||No|
|Operating System:||Easy fix?:||No|
|Pull Request or Patch supplied:||No||Resolution:|
|Crashes QGIS or corrupts data:||No||Copied to github as #:||27095|
Seems adding tasks to the task manager from Python using
task = TestTask('my task', 10) QgsApplication.taskManager().addTask(task)
works fine from Python console, but does not work from the QAction's slot.
Test plugin attached. Install and activate this plugin, run task from the menu "Plugins → Test → Test". Nothing happens, task does not started. Now open init.py and uncomment line 55 which just adds some dummy debug message to the log. Restart QGIS and run plugin again, task will be added and executed.
#1 Updated by Alexander Bruy about 3 years ago
There is also another issue — finished() is not executed (probably because Python object is garbage collected when out of scope). To test this uncomment lines 52-53 in the init.py and run plugin. After task completes mo messages will be added to the message log.
#2 Updated by Salvatore Larosa about 3 years ago
Hi Alexander, I don't know if this is or not a bug, but I would have defined the application taskmanager on
__init__ method of the testplugin. Code below works as expected.
class TestPlugin: def __init__(self, iface): self.iface = iface self.tm = QgsApplication.taskManager() def initGui(self): self.action = QAction('Test', self.iface.mainWindow()) self.iface.addPluginToMenu('Test', self.action) self.action.triggered.connect(self.run) def unload(self): self.iface.removePluginMenu('Test', self.action) def run(self): task = TestTask('my task', 10) self.tm.addTask(task) def complete(self): QgsMessageLog.logMessage('Task finished') def error(self): QgsMessageLog.logMessage('Error occurred')