gpt4 book ai didi

qt - PySide2 QThread 错误 : QThread: Destroyed while thread is still running

转载 作者:行者123 更新时间:2023-12-04 00:21:44 28 4
gpt4 key购买 nike

我是 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

最佳答案

说明:

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

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

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

另一方面,如果the Qt docs已审核:

void QThread::quit()

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

This function does nothing if the thread does not have an event loop.

(强调我的)

因此,如果没有事件循环,则 quit 方法将不执行任何操作。

解决方案:

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

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()

关于qt - PySide2 QThread 错误 : QThread: Destroyed while thread is still running,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60124633/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com