Changing GUI directly from Qthread PyQt 5

[亡魂溺海] 提交于 2020-01-01 19:08:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!