Changing GUI directly from Qthread PyQt 5

后端 未结 1 1616
离开以前
离开以前 2020-12-01 20:12

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):
    QT         


        
相关标签:
1条回答
  • 2020-12-01 21:02

    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)
        ...
    
    0 讨论(0)
提交回复
热议问题