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: mayaposch.wordpress.com/2011/11/01/… which explains why you shouldn't subclassQThread
. And: realpython.com/python-pyqt-qthread/… for a Python example.QgsTask
is 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) github.com/benwirf/basemap_2_geopackage/blob/main/…