title: what is the correct way to implement a QThread... (example please...)
body: <p>The Qt documentation for QThread says to create a class from QThread, and to implement the run method. </p>
  
  <p>Below is taken from the 4.7 Qthread documentation...</p>
  
  <blockquote>
    <p>To create your own threads, subclass QThread and reimplement run(). For example: </p>
  </blockquote>
  
  <pre><code> class MyThread : public QThread
   {
   public:
       void run();
   };
  
   void MyThread::run()
   {
       QTcpSocket socket;
       // connect QTcpSocket's signals somewhere meaningful
       ...
       socket.connectToHost(hostName, portNumber);
       exec();
   }
  </code></pre>
  
  <p>So in every single thread I've created, I've done just that and for most things it works just fine (I do not implement moveToThread(this) in any of my objects and it works great).</p>
  
  <p>I hit a snag last week (managed to get through it by working around where I created my objects) and found the <a href="http://blog.qt.io/blog/2010/06/17/youre-doing-it-wrong/" rel="noreferrer">following blog post</a>. Here is basically says that subclassing QThread really isn't the correct way to do it (and that the documentation is incorrect).</p>
  
  <p>This is coming from a Qt developer so at first glance I was interested and upon further reflection, agree with him. Following OO principles, you really only want to subclass a class to further enhance that class... not to just use the classes methods directly... thats why you instantiate... </p>
  
  <p>Lets say I wanted to move a custom QObject class to a thread... what would be the 'correct' way of doing it? In that blog post, he 'says' he has an example somewhere... but if someone could further explain it to me it'd be greatly appreciated!</p>
  
  <p><strong>Update:</strong></p>
  
  <p>Since this question gets so much attention, here is a copy and paste of the 4.8 documentation with the 'proper' way to implement a QThread.</p>
  
  <pre><code>class Worker : public QObject
   {
       Q_OBJECT
       QThread workerThread;
  
   public slots:
       void doWork(const QString &amp;parameter) {
           // ...
           emit resultReady(result);
       }
  
   signals:
       void resultReady(const QString &amp;result);
   };
  
   class Controller : public QObject
   {
       Q_OBJECT
       QThread workerThread;
   public:
       Controller() {
           Worker *worker = new Worker;
           worker-&gt;moveToThread(&amp;workerThread);
           connect(workerThread, SIGNAL(finished()), worker, SLOT(deleteLater()));
           connect(this, SIGNAL(operate(QString)), worker, SLOT(doWork(QString)));
           connect(worker, SIGNAL(resultReady(QString)), this, SLOT(handleResults(QString)));
           workerThread.start();
       }
       ~Controller() {
           workerThread.quit();
           workerThread.wait();
       }
   public slots:
       void handleResults(const QString &amp;);
   signals:
       void operate(const QString &amp;);
   };
  </code></pre>
  
  <p>I still believe that it is worthwhile to point out that they include an extra <code>Worker::workerThread</code> member that is unnecessary and is never used in their example. Remove that piece and it is a proper example of how to do threading in Qt.</p>