I'm using QThread to run processingBar and heavy job so the user will know it's loading.
Everything works fine until the job is done then the map is stuck and not reloading.
It looks like I need to reload Qgis somehow so the map that i use will work well after the thread is done
This is what I tried:
def onStart(self):
self.dlg.progressBar.setRange(0, 0)
self.myLongTask.start()
def onFinished(self):
self.dlg.progressBar.setRange(0, 1)
def run(self):
self.dlg.progressBar.setRange(0, 1)
self.dlg.pushButton.clicked.connect(self.onStart)
self.myLongTask = workThread()
self.myLongTask.taskFinished.connect(self.onFinished)
class workThread(QtCore.QThread):
taskFinished = QtCore.pyqtSignal()
def run(self):
time.sleep(3)
self.taskFinished.emit()
Update: I try using QObject as showed here:https://realpython.com/python-pyqt-qthread/
This is how my run look like:
self.dlg.progressBar.setRange(0, 1)
self.thread=QThread()
self.indexThread=IndexThread()
self.indexThread.moveToThread(self.thread)
self.thread.started.connect(self.indexThread.run)
self.indexThread.taskFinished.connect(self.thread.quit)
self.indexThread.taskFinished.connect(self.indexThread.deleteLater)
self.indexThread.taskFinished.connect(self.onFinished)
self.thread.finished.connect(self.thread.deleteLater)
self.dlg.indexButton.clicked.connect(self.onStart)
On start:
def onStart(self):
print("test")
self.thread.start()
self.dlg.progressBar.setRange(0, 0)
On finished:
def onFinished(self):
print("finishing")
self.dlg.progressBar.setRange(0, 1)
and I get the same result. Everything works but the used map on QGIS won't reload and get stuck. Any idea?


quit()ordeleteLater()on your thread object but... please read: https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/ which explains why you shouldn't subclassQThread. And: https://realpython.com/python-pyqt-qthread/#using-qthread-to-prevent-freezing-guis for a Python example. – Ben W Jan 03 '22 at 09:57QThread. Please look at the links I provided! – Ben W Jan 03 '22 at 10:01QThreadin a minimal Python plugin, which you can install and test. I honestly can't say why your plugin is not working because, thus far you have only shown us snippets of your code. In your updated answer you haven't shown us the contents of yourIndexThreadworker class. Maybe you are trying to do something in a background thread which is not thread safe like loading layers or interacting directly (not via signals) with the project or main gui??! – Ben W Jan 04 '22 at 10:40QgsTaskis a better option for threading in a QGIS plugin. I have used it sucessfully in a (not-published) plugin (but you can inspect the source code on my github account) https://github.com/benwirf/basemap_2_geopackage/blob/main/basemap_2_geopackage.py – Ben W Jan 04 '22 at 10:42