问题
Hi i have send to my Thread worker link to GUI and update GUI info directly from QThread. Like here:
class Worker(QThread):
def __init__(self, ui):
QThread.__init__(self)
self.running = False
self.ui = ui
def run(self):
self.running = True
while self.running:
info = self.check_info()
rows = len(info)
self.ui.tableWidget.setRowCount(rows)
self.ui.tableWidget.setColumnCount(6)
...
In main QMainWindow
i just add
def __init__(self, parent=None):
.......
self.myworker = Worker(self.ui)
.....
Is such solution very bad in PyQt5? I am new in PyQt. Thx. If my solution is bad plz help me to fix it.
回答1:
You can not and can not update the GUI from a secondary thread, you must do it through signals and slots:
For this we are going to create a signal through the class pyqtSignal()
, and we will indicate the types of arguments, then we connect it to the slot that we want, as I show below:
Thread:
class Worker(QThread):
list_of_dict_signals = pyqtSignal(list)
str_signal = pyqtSignal(str)
def __init__(self, parent=None):
QThread.__init__(self, parent)
self.running = False
def run(self):
self.running = True
while self.running:
info = self.check_info()
self.list_of_dict_signals.emit(info)
self.str_signal.emit("Requesting info")
GUI:
def __init__(self, parent=None):
.......
self.myworker = Worker(self)
self.myworker.list_of_dict_signals.connect(self.onDataFromThread)
self.myworker.str_signal.connect(self.ui.statusbar.showMessage)
self.myworker.start()
def onDataFromThread(self, info):
rows = len(info)
self.ui.tableWidget.setRowCount(rows)
...
来源:https://stackoverflow.com/questions/45665787/changing-gui-directly-from-qthread-pyqt-5