title: How to quit the event loop of a worker QThread
body: <p>I'm writing an application with a button to start/stop a worker thread (which implements <code>QThread</code>).
  The worker thread keeps scheduling a job every few milliseconds.
  To terminate the worker thread, I'm calling <code>worker.quit()</code> (<code>worker.exit(0)</code> doesn't work either) method from the GUI thread and waiting the <code>finished</code> signal to be fired.</p>
  <p>The problem is that even though the <code>finished</code> signal is fired, the thread isn't terminated.</p>
  <p>Here is a minimal example:
  <a href="https://gist.github.com/NawfelBgh/941babdc011f07aa4ab61570d7b88f08" rel="nofollow noreferrer">https://gist.github.com/NawfelBgh/941babdc011f07aa4ab61570d7b88f08</a></p>
  <p><a href="https://i.stack.imgur.com/hHfZk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hHfZk.png" alt="screenshot of the minimal example" /></a></p>
  <h3>Edit</h3>
  <p>My interpretation of what happened was wrong: <strong>The worker thread was being terminated</strong> but the method <code>iter</code> was getting executed in the main thread as said by @sergey-tachenov and as confirmed from the logging generated with the code:</p>
  <pre><code>void run() {
      std::cout &lt;&lt;&quot;From worker thread: &quot;&lt;&lt;QThread::currentThreadId() &lt;&lt; std::endl;
  ...
  void iter() {
      std::cout &lt;&lt;&quot;From thread: &quot;&lt;&lt;QThread::currentThreadId() &lt;&lt; std::endl;
  ...
  void MainWindow::on_pushButton_clicked()
  {
      std::cout &lt;&lt;&quot;From main thread: &quot;&lt;&lt;QThread::currentThreadId() &lt;&lt; std::endl;
  </code></pre>
  <p>I switched to a <a href="https://gist.github.com/NawfelBgh/4e7b98b0151e483129517674d29a7c5f" rel="nofollow noreferrer">different design</a> which doesn't rely on <code>QTimer</code>s. But I didn't submit it as an answer since the title of this question is &quot;How to quit the event loop of a worker QThread&quot;.</p>