PySide2 QThread 错误:QThread:线程仍在运行时销毁 [英] PySide2 QThread Error: QThread: Destroyed while thread is still running

查看:419
本文介绍了PySide2 QThread 错误:QThread:线程仍在运行时销毁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 PySide2 的新手.我只是想启动一个示例应用程序并在应用程序启动时启动一个线程,并希望在应用程序关闭时停止该线程.当我关闭应用程序时,出现以下错误:QThread:在线程仍在运行时销毁.sample_ui.py 是我从 sample_ui.ui 转换而来的 python 文件

I am new to PySide2. I am just trying to launch a sample application and start a thread as the application starts and want to stop the thread as the application closes. When I am closing the application I am getting the following error: QThread: Destroyed while the thread is still running. The sample_ui.py is a python file that I converted from sample_ui.ui

代码:

import time
import sys
import sample_ui
from PySide2 import QtWidgets
from PySide2 import QtCore


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        main_window_ui = sample_ui.Ui_MainWindow()
        main_window_ui.setupUi(self)
        self.custom_thread = CustomThread()
        self.custom_thread.start()

    def closeEvent(self, event):
        self.custom_thread.stop()
        QtWidgets.QMainWindow.closeEvent(self, event)

class CustomThread(QtCore.QThread):
    def __init__(self):
        super(CustomThread, self).__init__()
    def run(self):
        while self.isRunning():
           print("Thread is Running")
           time.sleep(1)
    def stop(self):
        print("Thread Stopped")
        self.quit()




if __name__ == '__main__':
    app = QtWidgets.QApplication()
    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec_())

输出:

Thread is Running
Thread is Running
Thread is Running
Thread Stopped
QThread: Destroyed while thread is still running

推荐答案

说明:

默认情况下,QThread run() 方法具有以下实现:

Explanation:

By default the QThread run() method has the following implementation:

// https://github.com/qt/qtbase/blob/5.14.1/src/corelib/thread/qthread.cpp#L601
void QThread::run()
{
    (void) exec();
}

换句话说,run 方法执行一个事件循环,但是当覆盖该方法时,您将通过 while 循环删除事件循环.

In other words, the run method executes an event loop but when override that method you are removing the event loop by the while loop.

另一方面,如果Qt 文档被审查:

On the other hand if the Qt docs is reviewed:

告诉线程的事件循环退出并返回代码 0(成功).相当于调用 QThread::exit(0).

void QThread::quit()

Tells the thread's event loop to exit with return code 0 (success). Equivalent to calling QThread::exit(0).

如果线程没有事件循环,这个函数什么都不做.

(强调我的)

因此,如果没有事件循环,则退出方法将什么也不做.

So if there is no event loop then the quit method will do nothing.

一个可能的解决方案是使用 isInterruptionRequested()requestInterruption()因为第一个指示标志的状态,第二个改变该标志的状态.另一方面,您必须使用 等待线程完成执行wait() 方法:

A possible solution is to use isInterruptionRequested() and requestInterruption() since the first one indicates the state of the flag and the second one changes the state of that flag. On the other hand you have to wait for the thread to finish executing using the wait() method:

class CustomThread(QtCore.QThread):
    def run(self):
        while not self.isInterruptionRequested():
            print("Thread is Running")
            time.sleep(1)

    def stop(self):
        print("Thread Stopped")
        self.requestInterruption()
        self.wait()

这篇关于PySide2 QThread 错误:QThread:线程仍在运行时销毁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆