PyQt报错——QThread: Destroyed while thread is still running

        当前项目需要实现输出测试报告功能,通过按键的点击信号连接的槽,调用工程中文件输出类的全局变量中的方法,创建继承自QThread类的自定义类实现。

        报错原因:原先的写法并没有将文件输出类中的exportThread变量定义为类变量,而是定义在方法中。当方法被返回(return)后,exportThread变量超出其作用域并被销毁。如果此时线程仍在运行,就会出现 QThread: Destroyed while thread is still running 的错误。

修改后代码如下:

按键定义:

 def ButtonInit(self):
     self.btExport = QPushButton('Export')
     self.btExport.setFixedWidth(100)
     self.btExport.clicked.connect(self.ExportClicked)
     self.hBoxLayout.addWidget(self.btExport)

按键点击信号的槽:

 def ExportClicked(self)
    self.btExport.setEnabled(False)
    global.exportObject.Export(self)

按键使能方法:

def BTExportEnable(self):
    self.btExport.setEnabled(True)

文件输出类:

class Export():
    def __init__(self):
        self.exportThread = None
    def Export(self, exportDialog):
        self.exportThread = ExportThread()
        self.exportThread.finished.connect(self.ExportThreadComplete)
        self.exportThread.start()

测试数据输出报告线程类:

class ExportThread(QThread):
    buttonSignal = pyqtSignal()
    def __init__(self,parent=None):
        super(ExportThread, self).__init__()
    def run(self):
        self.buttonSignal.emit()

以下是一个简单的示例代码,演示如何在PyQt5中使用QThread类,并在QThread对象销毁之前停止线程以避免QThread: Destroyed while thread is still running报警。 ```python import sys import time from PyQt5.QtCore import QThread, pyqtSignal, pyqtSlot, Qt from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton class MyThread(QThread): finished = pyqtSignal() message = pyqtSignal(str) def __init__(self): super().__init__() self.m_stop = False def stop(self): self.m_stop = True def run(self): while not self.m_stop: self.message.emit("Thread is running...") time.sleep(1) self.message.emit("Thread is stopped!") self.finished.emit() class MainWindow(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): self.label = QLabel(self) self.label.setGeometry(50, 50, 200, 30) self.button = QPushButton('Stop', self) self.button.setGeometry(50, 100, 100, 30) self.button.clicked.connect(self.stopThread) self.thread = MyThread() self.thread.message.connect(self.updateLabel) self.thread.finished.connect(self.threadFinished) self.thread.start() @pyqtSlot(str) def updateLabel(self, text): self.label.setText(text) def stopThread(self): self.thread.stop() @pyqtSlot() def threadFinished(self): self.thread.quit() self.thread.wait() print("Thread is finished!") self.close() if __name__ == '__main__': app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec_()) ``` 在上面的示例代码中,我们定义了一个MyThread类继承自QThread,在run()函数中实现了线程的执行逻辑,并在stop()函数中发送停止信号给线程。我们还定义了一个message信号,用于在UI线程中更新标签的文本。 在主窗口类中,我们创建了一个标签和一个按钮,并将按钮的clicked信号连接到stopThread()函数。在initUI()函数中,我们创建了一个MyThread对象并启动它,并将message信号连接到updateLabel()函数,将finished信号连接到threadFinished()函数。 在stopThread()函数中,我们发送停止信号给线程。在threadFinished()函数中,我们先使用quit()函数停止线程的事件循环,然后使用wait()函数等待线程完成,并在完成后关闭主窗口。 这样,我们就能够在QThread对象销毁之前正确地停止线程,避免了QThread: Destroyed while thread is still running报警。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值