I'm actually trying to merge one of my python program inside a QGIS 3 plugin. But I'm unable to run my simulation without freezing UI.
I tried several methods, first just calling a function on click, then I tried using QThread but only found examples for Qt4, and GUI froze again. Finally I tried to rewrite a QgsTask object following the PyQgis Cookbook.
But I always end with a frozen GUI, no matter how I write this class... I tried:
An object inheriting from QgsTask only which run function is calling the run() function from my simulation
Multiple inheritance : a class inheriting both from my Simulation object and the QgsTask object, rewrote the run() with the one from my simulation... I tested this double inheritance in a dummy class outside of gui and it is working all right, as my Simulation class.
I'm not sure but it looks like multiple mains() from QGIS and my own objects is kind of a mess. It's freezing but in facts simulation is not running and outputdir isn't created.
Here is my class:
class QGISimulationTask(QgsTask, Simulation): def __init__(self, params): desc = 'Simu ' + str(params.split()[2:5]) Simulation.__init__(self, user_params=params, interface='qgis') QgsTask.__init__(self, description=desc, flags=QgsTask.CanCancel) def run(self): self.init_project() self.beginTime = time() preLog = 0 c = 0 for year in range(2015, self.finalYear + 1): ... def finished(self, success): if success: self.compute_results() else: QgsMessageLog.logMessage('Échec de la simulation', Qgis.Critical) def cancel(self): super(QgsTask, self).cancel() self.reset() QgsMessageLog.logMessage('Simulation annulée', Qgis.Info)
Other classes, in separated py files :
- Widget inheriting Ui
- SimlationTask inheriting from QgsTask and Simulation
- PluginClass with a start() method to create a SimulationTask object on click.
how I'm starting a task :
self.task = QGISimlationTask(params, self.dockwidget.Simu_progressBar) # QgsApplication.taskManager().addTask(self.task)
Seems like it's freezing during init (before I add it to the manager...)
Any ideas ?