Skip to content Skip to sidebar Skip to footer

Pyqt5 Cannot Update Progress Bar From Thread And Received The Error "cannot Create Children For A Parent That Is In A Different Thread"

I am retired and teaching myself to write code. I am working on a program that requires a thread to operate in the background (GUI developed with PYQT5) so I can still use the GUI

Solution 1:

Your code has several errors since PyQt has certain minimum rules:

  • The thread where the GUI application is created is called GUI thread because there must be created and live any graphic component, but you are creating unnecessary RPTApp inside the QThread, I say unnecessary since the thread is created inside RPTApp so it does not it is necessary to create another.

  • Another error is in the emission of the signal in the wire, you do not have to call the function that uses the data that emits the signal but you must connect it to the slot. The application will be in charge of transporting the data and invoking the slot.

All of the above is corrected in the following section:

classExecuteSession(QThread):
    PBValueSig = pyqtSignal(int)
    [...]
    defrun(self):
        i = 0while i <= self.dur:
            print(i)
            i = i + 1
            self.PBValueSig.emit(i)
            time.sleep(1)


classRPTApp(QMainWindow, Ui_MainWindow):
    [..]
    defPB(self):
        dur = 10
        self.progressBar.setMinimum(0)
        self.progressBar.setMaximum(dur)
        self.progressBar.setValue(0)
        # thread
        self.exeThread = ExecuteSession(dur)
        self.exeThread.PBValueSig.connect(self.updateProgressBar)
        self.exeThread.start()

    @pyqtSlot(int)defupdateProgressBar(self, value):
        print("INT + " + str(value))
        self.progressBar.setValue(value)

Note: It is not recommended to use int as a variable since it is the name of a preloaded function, there are thousands of other names.

Post a Comment for "Pyqt5 Cannot Update Progress Bar From Thread And Received The Error "cannot Create Children For A Parent That Is In A Different Thread""