我有一个QThread继承的类,用于耗时操作
class ChargingThread(QtCore.QThread):
_res = pyqtSignal(str)
def __init__(self):
super(ChargingThread,self).__init__()
def run(self):
在主类中调用该类时报错
a = ChargingThread()
a._res.connect(self.show_result)
a.start()
error:
QThread :Destroyed while thread is still running
原因是在MyWidget中,a是一个局部变量,当mousePressEvent函数结束后,它的生命周期也都结束了,但是这个线程里的程序很有可能还没有运行完,所以才会报错,解决方案如下:
self.a = ChargingThread()
self.a._res.connect(self.show_result)
self.a.start()