You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyQt6中QThread线程销毁异常的多场景技术问询

PyQt6 QThread 「Destroyed while thread is still running」异常场景疑问

环境信息:Mac 13、Python 3.9.6,TestThread通过pyqtSignal循环输出0-9999的数字,每次间隔1秒,正常需运行至输出9999才会退出。实践中遇到「QThread: Destroyed while thread is still running」异常,针对以下四种使用场景存在疑问:

场景1:不给TestThread设置parent时,提前终止程序(Mac下按Meta+q)运行正常

对应代码:

# play function
def play(self):
    print("Create a thread")
    self.tt = TestThread()
    self.tt.output_signal.connect(
        self.output_print
    )
    print('create thread success')
    self.tt.start()

场景2:给TestThread设置parent=self时,提前终止程序会触发上述异常

对应代码:

def play(self):
    print("Create a thread")
    self.tt = TestThread(parent=self)
    self.tt.output_signal.connect(
        self.output_print
    )
    print('create thread success')
    self.tt.start()

场景3:在MW中将TestThread作为局部变量且设置parent=self时,提前终止程序会触发上述异常

对应代码:

def play(self):
    print("Create a thread")
    tt = TestThread(parent=self)
    tt.output_signal.connect(
        self.output_print
    )
    print('create thread success')
    tt.start()

场景4:在MW中将TestThread作为局部变量且不设置parent时,提前终止程序会触发上述异常,且QMainWindow窗口无法显示

对应代码:

# play function
def play(self):
    print("Create a thread")
    tt = TestThread()
    tt.output_signal.connect(
        self.output_print
    )
    print('create thread success')
    tt.start()

内容的提问来源于stack exchange,提问作者ssayno

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 11:52:44