title: Minimal Example to use Qt Threads?
body: <h2>TL;DR:</h2>
  
  <p>I was looking for a concise example of Qt threading, and all I found was complex "eye-candy" that claimed to show how QThreads work, but they were simply too much to figure out.</p>
  
  <p>I accepted Zen's answer because he showed me the exact thing that my attempts were missing, then added my own to be the example that I wanted to see.  Click here to skip to it: <a href="https://stackoverflow.com/a/34561122/3491308">https://stackoverflow.com/a/34561122/3491308</a></p>
  
  <p>My original question is as follows:</p>
  
  <hr>
  
  <p>It seems I must be missing something, but I just can't seem to make this work like I think it ought to.  My complete application needs to have two or three threads, all of which start (almost) together and run forever:</p>
  
  <ul>
  <li>The GUI
  
  <ul>
  <li>Loads a bunch of objects from files before starting the other two threads to process them.</li>
  </ul></li>
  <li>A realtime processor
  
  <ul>
  <li>Has TimeCriticalPriority and a periodic QTimer "interrupt" in an attempt to use a PC as a much more powerful embedded system than what I started with.  This project started as entirely embedded and quickly became too involved to manage there.</li>
  </ul></li>
  <li>A USB HID driver
  
  <ul>
  <li>Connects to the part of the project that is still embedded.</li>
  </ul></li>
  </ul>
  
  <p>The core functionality, of course, is in the realtime processing thread, and the other two are simply to adjust how it works.  So there are processing objects being created, modified, and destroyed while the realtime processor is running, based on the user's actions in the GUI, and the data inside some of those objects is being produced and consumed by USB also.</p>
  
  <p>I keep looking for examples of how to use threads in Qt, but I keep getting complex "eye candy"-type applications that clutter up the threading part.  I do my best to try and interpret them and write my own, but I keep getting segfaults in Qt itself and my small "playground" project says that I'm not even getting a second thread:</p>
  
  <pre><code>#ifndef MAIN_H
  #define MAIN_H
  
  #include &lt;QtWidgets&gt;
  
  class MainWindow : public QMainWindow
  {
      Q_OBJECT
  public:
      MainWindow(QWidget* parent = 0);
      ~MainWindow() {}
  private:
      QObject* mythingy;
  private slots:
      void deleteObject(QObject* thingy);
  };
  
  class Worker : public QObject
  {
      Q_OBJECT
  public:
      Worker(QObject* thingy, QObject* parent = 0);
  private:
      QObject* mythingy;
  signals:
      void deleteObject(QObject* thingy);
  private slots:
      void doWork();
  };
  
  #endif // MAIN_H
  
  /***************
  *** main.cpp ***
  ***************/
  #include "main.h"
  #include &lt;QApplication&gt;
  
  int main(int argc, char* argv[])
  {
      QApplication a(argc, argv);
      MainWindow w;
      w.show();
      return a.exec();
  }
  
  
  
  MainWindow::MainWindow(QWidget* parent)
      : QMainWindow(parent)
  {
      mythingy = new QObject(this);
      QThread* thisthread = this-&gt;thread();
      QThread* mainthread = QCoreApplication::instance()-&gt;thread();
      //breakpoint here to check thisthread and mainthread
      Worker* worker = new Worker(mythingy, this);
      connect(worker, SIGNAL(deleteObject(QObject*)), this, SLOT(deleteObject(QObject*)));
  }
  
  void MainWindow::deleteObject(QObject* thingy)
  {
      QThread* thisthread = this-&gt;thread();
      QThread* mainthread = QCoreApplication::instance()-&gt;thread();
      //breakpoint here to check thisthread and mainthread
      delete thingy;
  }
  
  
  
  Worker::Worker(QObject* thingy, QObject* parent)
      : QObject(parent)
  {
      mythingy = thingy;
      QThread* thread = new QThread(this);
      this-&gt;moveToThread(thread);
  
      //use a timer to allow the constructor to exit
      QTimer* timer = new QTimer(this);
      timer-&gt;setSingleShot(true);
      timer-&gt;start(1000);
      connect(timer, SIGNAL(timeout()), this, SLOT(doWork()));
  
      QThread* thisthread = this-&gt;thread();
      QThread* mainthread = QCoreApplication::instance()-&gt;thread();
      //breakpoint here to check thisthread and mainthread
      thread-&gt;start();
  }
  
  void Worker::doWork()
  {
      QThread* thisthread = this-&gt;thread();
      QThread* mainthread = QCoreApplication::instance()-&gt;thread();
      //breakpoint here to check thisthread and mainthread
      deleteObject(mythingy);
  }
  </code></pre>
  
  <p>If someone can post an example of how to do QThreads right, given my description of the project, and with as little clutter as possible (preferably shorter than mine if possible), I'd greatly appreciate it.</p>
  
  <hr>
  
  <h2>Edit:</h2>
  
  <p>Based on Zen's answer, this seems to work too:</p>
  
  <pre><code>MainWindow::MainWindow(QWidget* parent)
      : QMainWindow(parent)
  {
      mythingy = new QObject(this);
      QThread* thisthread = this-&gt;thread();
      QThread* mainthread = QCoreApplication::instance()-&gt;thread();
      Worker* worker = new Worker(mythingy, this);
  }
  
  Worker::Worker(QObject* thingy, QObject* parent)
      : QObject(0)    //no real parent, so we can move to a different thread
  {
      mythingy = thingy;
  
      QTimer* timer = new QTimer(this);
      timer-&gt;setSingleShot(true);
      timer-&gt;start(1000);
      connect(timer, SIGNAL(timeout()), this, SLOT(doWork()));
  
      QThread* thread = new QThread(parent);
      this-&gt;moveToThread(thread);
      thread-&gt;start();
  }
  
  void Worker::doWork()
  {
      QThread* thisthread = this-&gt;thread();
      QThread* mainthread = QCoreApplication::instance()-&gt;thread();
      delete mythingy;
  }
  </code></pre>
  
  <p>Still moving myself inside the constructor, and now deleting the object directly instead of telling the owner's thread to do it.  (remember in the full project that the owner has already marked the object for deletion but refrained from actually doing so because the other thread might be using it)</p>
  
  <p>Anything wrong with that?</p>