I'm testing some gui interactions between threads to apply it to my program, but the program just suddenly crashes if I try it.
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_()
As you can see, thread emitts "setparent" signal and main thread connects it to its setparent slot. However, it just crashes without any traceback even though I only try setting parent of widget.
The GUI should not be updated directly in another thread that is not the main thread, and this also means that you should not create the widgets in another thread
read the following: http://doc.qt.io/archives/qt-4.8/thread-basics.html#gui-thread-and-worker-thread