Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PySide2 QThread错误:QThread:在线程仍在运行时已销毁_Qt_Pyqt_Pyqt5_Pyside2 - Fatal编程技术网

PySide2 QThread错误:QThread:在线程仍在运行时已销毁

PySide2 QThread错误:QThread:在线程仍在运行时已销毁,qt,pyqt,pyqt5,pyside2,Qt,Pyqt,Pyqt5,Pyside2,我是PySide2的新手。我只是尝试启动一个示例应用程序,在应用程序启动时启动一个线程,并希望在应用程序关闭时停止该线程。关闭应用程序时,出现以下错误: QThread:在线程仍在运行时销毁。 sample_ui.py是我从sample_ui.ui转换而来的python文件 代码: import time import sys import sample_ui from PySide2 import QtWidgets from PySide2 import QtCore class Mai

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

代码:

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

输出:

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::quit()
如果线程没有事件循环,则不会执行任何操作。您应该在
run()
中使用一个“关闭”
的标志,而不是检查
QThread::isRunning()
返回的内容。此外,建议始终添加具有特定时间限制的
QThread::wait()
,然后添加
QThread::terminate()
(作为备份计划)

就像一个实验一样,在调用
stop()
函数并查看返回结果后,添加对
QThread::isRunning()
的检查。

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

//https://github.com/qt/qtbase/blob/5.14.1/src/corelib/thread/qthread.cpp#L601
void QThread::run()
{
(void)exec();
}
换句话说,run方法执行一个事件循环,但是当重写该方法时,while循环将删除该事件循环

另一方面,如果审查:

void QThread::quit() 通知线程的事件循环退出,返回代码为0(成功)。 相当于调用QThread::exit(0)

如果线程没有事件循环,则此函数不执行任何操作

(强调矿山)

因此,如果没有事件循环,那么quit方法将什么也不做

解决方案: 一个可能的解决方案是使用和,因为第一个指示标志的状态,第二个改变标志的状态。另一方面,您必须等待线程使用以下方法完成执行:

类CustomThread(QtCore.QThread):
def运行(自):
而不是自身。isInterruptionRequested():
打印(“线程正在运行”)
时间。睡眠(1)
def停止(自):
打印(“线程停止”)
self.requestInterruption()
self.wait()

非常感谢,@eyllanesc。我试过你的方法,它奏效了。我非常感谢您花时间解释QThread run()方法。如果您能让我知道一些关于PySide2和使用QtDesigner的资源或教程,那将是一个很大的帮助。谢谢。@PratikTayshete 1)如果您想了解更多关于QThread的解释,那么您应该阅读文档,如果您没有向我指出一个精确的问题,那么我将无法回答您。2) 使用Qt文档,不知道教程或类似内容,我认为Google可以成为搜索此类教程的好引擎。deleteLater()方法与此相关吗