为什么不能在类的成员函数中初始化QThread?

2024-06-10 17:52:10 发布

您现在位置:Python中文网/ 问答频道 /正文

代码如下:

##########################################################
######## Version 1 (works)
##########################################################
#!/usr/bin/env python3


from ui.qthreadtest import Ui_QthreadTest
from PySide import QtCore, QtGui
import time

class Md(QtGui.QDialog):
    def __init__(self):
        super().__init__()
        self.prcsbtn = QtGui.QPushButton("Process")
        self.prcsedit = QtGui.QLineEdit()
        layout = QtGui.QHBoxLayout()
        layout.addWidget(self.prcsedit)
        layout.addWidget(self.prcsbtn)
        self.setLayout(layout)
        self.prcsbtn.clicked.connect(self.work)

        self.wt = Worker()

    def work(self):
        self.wt.start()
        # the DirectConnection option demands the slot to be triggered immediately
        self.wt.workFinished.connect(self.changeText, QtCore.Qt.DirectConnection)

    def changeText(self):
        self.prcsedit.setText("Work finished!")

class Worker(QtCore.QThread):
    workFinished = QtCore.Signal()
    def __init__(self):
        super().__init__()

    def run(self):
        time.sleep(2)
        self.workFinished.emit()

import sys
app = QtGui.QApplication(sys.argv)
md = Md()
md.show()
sys.exit(app.exec_())


##########################################################
######## Version 2 (doesn't work)    
##########################################################

#!/usr/bin/env python3


from ui.qthreadtest import Ui_QthreadTest
from PySide import QtCore, QtGui
import time

class Md(QtGui.QDialog):
    def __init__(self):
        super().__init__()
        self.prcsbtn = QtGui.QPushButton("Process")
        self.prcsedit = QtGui.QLineEdit()
        layout = QtGui.QHBoxLayout()
        layout.addWidget(self.prcsedit)
        layout.addWidget(self.prcsbtn)
        self.setLayout(layout)
        self.prcsbtn.clicked.connect(self.work)


    def work(self):
        # the worker thread initialized in a member function won't work.
        wt = Worker()
        wt.start()
        # the DirectConnection option demands the slot to be triggered immediately
        wt.workFinished.connect(self.changeText, QtCore.Qt.DirectConnection)

    def changeText(self):
        self.prcsedit.setText("Work finished!")

class Worker(QtCore.QThread):
    workFinished = QtCore.Signal()
    def __init__(self):
        super().__init__()

    def run(self):
        time.sleep(2)
        self.workFinished.emit()

import sys
app = QtGui.QApplication(sys.argv)
md = Md()
md.show()
sys.exit(app.exec_())

版本1工作正常,版本2给出此错误(按下过程按钮时立即破碎):

^{pr2}$

Tags: thefromimportselfinitdefsyswork
1条回答
网友
1楼 · 发布于 2024-06-10 17:52:10

这是因为与threading.Thread对象不同,QThread对象一旦超出范围就会被销毁。在您的示例中,wtwork结束时超出范围,因此对象被销毁。为了避免这种情况,您需要保留对wt的持久引用。一种方法是使其成为实例变量:

def work(self):
    self.wt = Worker()
    self.wt.start()
    # the DirectConnection option demands the slot to be triggered immediately
    self.wt.workFinished.connect(self.changeText, QtCore.Qt.DirectConnection)

相关问题 更多 >