pyqt线程分离使程序崩溃

pyqt thread seperating makes the program crash

我正在测试线程之间的一些 gui 交互以将其应用于我的程序,但如果我尝试它,程序就会突然崩溃。

from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Window(QWidget):
    def __init__(self):
        super().__init__()
        qv = QVBoxLayout()
        self.board = QLabel(self)
        self.board.setFixedSize(300, 300)
        self.board.setStyleSheet("background-color:yellow;")
        qv.addWidget(self.board)
        self.setLayout(qv)
        self.show()
        self.thread = Thread()
        self.thread.setparent.connect(self.setparent)
        self.thread.start()

    def __del__(self):
        self.thread.exit(0)

    @pyqtSlot(QWidget)
    def setparent(self, widget):
        widget.setParent(self)

class Thread(QThread):
    setparent = pyqtSignal(QWidget)
    def __init__(self):
        super().__init__()

    def run(self):
        label = QLabel()
        self.setparent.emit(label)

    def __del__(self):
        print("Thread terminated")


if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    form = Window()
    app.exec_()

如您所见,线程发出 "setparent" 信号,主线程将其连接到其 setparent 插槽。 但是,即使我只尝试设置小部件的父级,它也会在没有任何回溯的情况下崩溃。

GUI不应该在非主线程的另一个线程中直接更新,这也意味着你不应该在另一个线程中创建小部件

阅读以下内容:http://doc.qt.io/archives/qt-4.8/thread-basics.html#gui-thread-and-worker-thread