How to properly quit a QThread in PyQt5 when using moveToThread
<p>I am trying to quit a thread after it is done processing. I am using moveToThread. I'm trying to quit the worker thread from the main thread by calling self.thread.quit() in the slot. And that's not working.</p>

<p>I've found several examples of starting the thread using moveToThread, such as this one. But I can't find how to quit one.</p>

<pre><code>from PyQt5.QtCore import QObject, QThread
from PyQt5.QtCore import pyqtSlot, pyqtSignal
from PyQt5.QtWidgets import QMainWindow

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        print("Base init")
        self.start_thread()

    @pyqtSlot(int)
    def onFinished(self, i):
        print("Base caught finished, {}".format(i))
        self.thread.quit()
        print('Tried to quit thread, is the thread still running?')
        print(self.thread.isRunning())

    def start_thread(self):
        self.thread = QThread()
        self.w = Worker()
        self.w.finished1[int].connect(self.onFinished)
        self.w.moveToThread(self.thread)
        self.thread.started.connect(self.w.work)
        self.thread.start()

class Worker(QObject):
    finished1 = pyqtSignal(int)

    def __init__(self):
        super().__init__()
        print("Worker init")

    def work(self):
        print("Worker work")
        self.finished1.emit(42)


if __name__ == "__main__":
    import sys
    from PyQt5.QtWidgets import QApplication

    app = QApplication(sys.argv)
    mw = MainWindow()
    mw.show()

sys.exit(app.exec_())
</code></pre>

<p>This is the output from all my print functions (without the color of course): </p>

<pre><code>Base init
Worker init
Worker work
Base caught finished, 42
Tried to quit thread, is the thread still running?
True
</code></pre>