我正在寻找一种正确退出我的应用程序的方法。当我退出时,会出现一个错误,提示QThread: Destroyed while thread is still running
。我有一个线程用于将输出发送到QTextBrowser
。正确的退出方式应该是什么?以下是我的代码:
class LogReceiver(QtCore.QObject):
mysignal = QtCore.Signal(str)
def __init__(self, queue, *args, **kwargs):
QtCore.QObject.__init__(self, *args, **kwargs)
self.queue = queue
def run(self):
while True:
text = self.queue.get()
self.mysignal.emit(text)
if __name__ == '__main__':
queue = Queue()
thread = QtCore.QThread()
my_receiver = MyReceiver(queue)
app = QApplication(sys.argv)
window = MainWindow()
window.show()
my_receiver.mysignal.connect(window.append_text)
my_receiver.moveToThread(thread)
thread.started.connect(my_receiver.run)
thread.start()
sys.exit(app.exec_())
在退出时,thread
是否应该被终止?请注意,self.queue.get()
会阻塞并等待文本。
谢谢
timeout=1.0
,但是在thread.wait()
处会卡住。 - Kar