Qthread - return value from thread to main

Hi all,
I’m writing a pyqt5 projects with threads. I would like receive a variable from thread function when finished emit is received.

I tried with following simply code:

class connectionToMachine(QtCore.QThread):
    finished = QtCore.pyqtSignal(object)
    def __init__(self):
        QtCore.QThread.__init__(self)
    def run(self):
        self.checkConnection = False
        def check():
            out = True
            return out
        check = connection()
        self.finished.emit(check)

class Ui(QMainWindow):
    checkCode = False
    def __init__(self):
        super().__init__()
        ...

    def onCheckThreadFinished(self, check):
         print(check)

    def onConnectionClicked(self):
        self.threadConnection = QThread()
        self.workerConnection = connectionToMachine()
        self.workerConnection.moveToThread(self.threadConnection)
        self.threadConnection.finished.connect(self.onCheckThreadFinished)
        self.threadConnection.started.connect(self.workerConnection.run)
        self.threadConnection.started.connect(self.threadConnection.quit)
        self.threadConnection.started.connect(self.workerConnection.deleteLater)
        self.threadConnection.started.connect(self.threadConnection.deleteLater)
        self.threadConnection.start()

but doesn’t work

Maybe is possible change checkCode variable in thread function and return new value when thread finished is emitted?

thank you very much

First of all, ensure that the connectionToMachine thread is run – perhaps by placing a debug statement in its run method.
Then, check that the code in onConnectionClicked is doing what you expect it to. I think there’s something wrong with the use of the threadConnection thread to host the workerConnection thread. The moveToThread method is used to move other kinds of QObject instances between threads, not QThread objects themselves.

I think you should just use workerConnection on its own:

    def onConnectionClicked(self):
        self.workerConnection = connectionToMachine()
        self.workerConnection.finished.connect(self.onCheckThreadFinished)
        self.workerConnection.started.connect(self.workerConnection.run)
        self.workerConnection.started.connect(self.threadConnection.quit)
        self.workerConnection.started.connect(self.workerConnection.deleteLater)
        self.workerConnection.started.connect(self.threadConnection.deleteLater)
        self.workerConnection.start()

That should do something closer to what you expect.