I'm rewriting a plugin to use QgsTask with this as my first guideline and the update mentioned in this post as a possible solution for my problem. My task runs fine in the background, but
def finished(self, result):
and
def completed(self, exception, result=None):
are never called. Anybody any ideas?
EDIT: added more code below
EDIT 2: found out that if there is an error in the method run it then actually reaches finished before it gives an error.
main_plugin.py
# Import link to Qt and QGIS
from qgis.PyQt.QtGui import *
from qgis.PyQt.QtCore import *
from qgis.PyQt.QtWidgets import QAction, QMenu, QFileDialog
from qgis.core import *
from qgis.gui import QgsMessageBar
# Import everything to create the plugin
from . import resources
from .plugin_dialog import PluginDialog
from .gdal_raster import Raster
from .task_raster import CreateRasterTask
from .task_raster import DoStuff
# Import everything to run the plugin
import sys
import os
class RasterPreparePlugin(object):
def __init__(self, iface):
# save reference to the QGIS interface
self.iface = iface
def run(self):
self.dlg = PluginDialog()
self.dlg.show()
# Run the dialog event loop
result = self.dlg.exec_()
if result:
layer = combobox.currentLayer()
path = self.dlg.ui.LineEditOutputLayer.text()
raster = Raster(layer, path, VERSION)
okay = DoStuff(raster, self.iface)
okay.do_subclassed_tasks()
task_raster.py
from qgis.core import (
QgsApplication,
QgsTask,
QgsMessageLog,
Qgis,
)
MESSAGE_CATEGORY = 'CreateRasterTask'
class CreateRasterTask(QgsTask):
def __init__(self, description, raster):
super(QgsTask, self).__init__(description, QgsTask.CanCancel)
self.exception = None
self.raster = raster
def run(self):
test = self.raster.task()
return test
def finished(self, result):
if result:
QgsMessageLog.logMessage(
'Task "{name}" completed'.format(
name=self.description()
),
MESSAGE_CATEGORY, Qgis.Success)
else:
if self.exception is None:
QgsMessageLog.logMessage(
'Task "{name}" not successful but without '\
'exception (probably the task was manually '\
'canceled by the user)'.format(
name=self.description()),
MESSAGE_CATEGORY, Qgis.Warning)
else:
QgsMessageLog.logMessage(
'Task "{name}" Exception: {exception}'.format(
name=self.description(),
exception=self.exception),
MESSAGE_CATEGORY, Qgis.Critical)
raise self.exception
def cancel(self):
QgsMessageLog.logMessage(
'Task "{name}" was canceled'.format(
name=self.description()),
MESSAGE_CATEGORY, Qgis.Info)
super().cancel()
class DoStuff:
def __init__(self, raster, iface):
self.raster = raster
self.iface = iface
self.task = CreateRasterTask('Test', self.raster)
def do_subclassed_tasks(self):
"""Do tasks using QgsTask subclass. """
QgsApplication.taskManager().addTask(self.task)
def stopped(self, task):
QgsMessageLog.logMessage(
'Task "{name}" was canceled'.format(
name=task.description()),
self.MESSAGE_CATEGORY, Qgis.Info)
def completed(self, exception, result=None):
if exception is None:
if result is None:
QgsMessageLog.logMessage(
'Completed with no exception and no result '\
'(probably manually canceled by the user)',
MESSAGE_CATEGORY, Qgis.Warning)
else:
QgsMessageLog.logMessage(
'Task "{name}" finished'.format(
name=task.description()),
self.MESSAGE_CATEGORY, Qgis.Info)
else:
QgsMessageLog.logMessage("Exception: {}".format(exception),
MESSAGE_CATEGORY, Qgis.Critical)
raise exception
gdal_raster.py
# Import link to QGIS
from qgis.core import (
QgsPointXY,
QgsExpressionContextScope,
QgsExpressionContextUtils,
QgsMessageLog,
Qgis,
)
# Import everything to run the plugin
import osgeo.gdal as gdal
import osgeo.osr as osr
import numpy as np
import datetime
import math
class Raster(object):
def __init__(self, layer, path, version):
self.layer = layer
self.path = path
self.version = version
def task(self):
QgsMessageLog.logMessage('Debug 5', 'MESSAGE_CATEGORY', Qgis.Success)
try:
# do stuff
return True
except:
return False
DoStuff.do_subclassed_tasks
is never called. So your task is not executed (or I miss something).