Bug report #19267

QgsTask does not starts after adding to the task manager

Added by Alexander Bruy over 5 years ago. Updated over 5 years ago.

Status:Closed
Priority:Normal
Assignee:-
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

Description

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.

tasktest.zip - Simple test plugin (1.3 KB) Alexander Bruy, 2018-06-26 07:13 AM

History

#1 Updated by Alexander Bruy over 5 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 over 5 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')

#3 Updated by Alexander Bruy over 5 years ago

  • Status changed from Open to Closed

Also available in: Atom PDF