I've got this part of code, which sometimes works and sometimes throws warning:
QThread: Destroyed while thread is still running
This part is in UiMainWindow class
obj_thread = QtCore.QThread()
def ok():
self.module_src_tree.setStyleSheet('background-color: #81F781')
obj_thread.quit()
def err():
self.module_src_tree.setStyleSheet('background-color: #F78181')
obj_thread.quit()
tmp = self.Temp(self, module_revision)
tmp.moveToThread(obj_thread)
tmp.finished.connect(ok)
tmp.error.connect(err)
obj_thread.started.connect(tmp.run)
obj_thread.start()
This is class in UiMainWindow class
class Temp(QtCore.QObject):
finished = QtCore.pyqtSignal()
error = QtCore.pyqtSignal()
def __init__(self, gui, module_revision):
QtCore.QObject.__init__(self)
self.gui = gui
self.module_revision = module_revision
def run(self):
try:
self.gui.dp.pack_module_source(self.gui.module_src_show_list, self.gui.module_src_pack_list,
path=str(self.gui.path_box.text()), revision=self.module_revision)
self.finished.emit()
except Exception as e:
self.error.emit()
raise e
What I am trying to do with this code - I want to zip some files without freezing main application. So I start new Thread which works in background. But I need functionality that a Widget changes it's color after the zipping is done to green or to red if something went wrong.
Maybe I am doing something wrong? Maybe that is not the way?
Most probles I've got with changing color part.
Best Regards,
Marek
@three_pineapples, seeems like it doesn't start at all sometimes. But there is no error/warning right now.
I have modified code to this:
class UiMainWindow(object):
# thread pool
thread_pool = [None, None, None, None]
This is before class constructor. Temp class stays the same as above, and Thread calling part of code looks like this right now:
self.thread_pool[3] = None
self.thread_pool[3] = QtCore.QThread()
def ok():
self.module_src_tree.setStyleSheet('background-color: #81F781')
self.thread_pool[3].quit()
def err():
self.module_src_tree.setStyleSheet('background-color: #F78181')
self.thread_pool[3].quit()
tmp = self.Temp(self, module_revision)
tmp.moveToThread(self.thread_pool[3])
tmp.finished.connect(ok)
tmp.error.connect(err)
self.thread_pool[3].started.connect(tmp.run)
self.thread_pool[3].start()
This issue occurs when the thread is garbage collected by Python.
You need to save a reference to your QThread
so it is not garbage collected. Just do self.obj_thread = QtCore.QThread()
If there is the possibility that multiple QThread
s will exist at once, and you are storing the references in the same variable, then you probably need to store the objects in a list instead. However, you will need to clear out objects from the list (so they are garbage collected) when a given thread has finished so that you don't introduce a memory leak to your application.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.