title: Simple multithreading with Qt: am I doing this right?
body: <p>I'm new to StackOverflow and wondering if I'm doing this right:</p>
  
  <p>I'm writing a simple Qt application to test multi-threading (something I am also completely new to). I made a MainWindow that contains widgets, and a class MyThread that subclasses QThread and overrides the run() method.</p>
  
  <p>The application simply displays two buttons, "Start Counter" and "Stop Counter", and a text field. When "start counter" is pressed, a worker thread is created and runs in the background, continuously incrementing a counter in a while loop and signaling the main thread (where the GUI is) with the updated value. When "Stop Counter" is pressed, a signal is sent to the main thread that stops the while loop, and the counter is stopped until "Start Counter" is pressed again.</p>
  
  <p>This works perfectly fine ... but is it the best way? I'm new at this, and read a lot of people saying "don't subclass QThread" and other people saying "subclass QThread", and it's a little bit confusing. If this isn't the best way to implement this sort of thing (run a computationally-intensive loop in a background thread with "start" and "stop" buttons), what is? If I'm doing it wrong, how do I do it right? I don't want to learn the wrong way.</p>
  
  <p>Thank you! And here's the code:</p>
  
  <p><strong>MyThread.h</strong></p>
  
  <pre><code>#ifndef MYTHREAD_H
  #define MYTHREAD_H
  
  #include &lt;QThread&gt;
  #include &lt;QMutex&gt;
  
  class MyThread : public QThread
  {
     Q_OBJECT
  
  public slots:
      void stopRunning();
  
  protected:
     virtual void run();
  
  signals:
     void signalValueUpdated(QString);
  
  private:
      bool isRunning;
  
  };
  </code></pre>
  
  <p><strong>MyThread.cpp</strong></p>
  
  <pre><code>#include "MyThread.h"
  #include &lt;QString&gt;
  
  void MyThread::run()
  {
      qDebug("Thread id inside run %d",(int)QThread::currentThreadId());
  
      static int value=0; //If this is not static, then it is reset to 0 every time this function is called.
      isRunning = 1;
      while(isRunning == 1)
      {
          QString string = QString("value: %1").arg(value++);
          sleep(1/1000); //If this isn't here, the counter increments way too fast and dies, or something; the app freezes, anyway.
  
          emit signalValueUpdated(string);       
      }            
  }
  
  void MyThread::stopRunning()
  {
      isRunning = 0;
  }
  </code></pre>
  
  <p><strong>MainWindow.h</strong></p>
  
  <pre><code>#ifndef MAINWINDOW_H
  #define MAINWINDOW_H
  
  #include &lt;QApplication&gt;
  #include &lt;QPushButton&gt;
  #include &lt;QHBoxLayout&gt;
  #include &lt;QLineEdit&gt;
  #include "MyThread.h"
  
  class MainWindow : public QWidget
  {
    Q_OBJECT
  
    public:
      MainWindow(QWidget *parent = 0);
  
    private:
      //Widgets
      QHBoxLayout * boxLayout;
      QPushButton * startButton;
      QPushButton * stopButton;
      QLineEdit * lineEdit;
  
      MyThread thread;
  };
  
  #endif
  </code></pre>
  
  <p><strong>MainWindow.cpp</strong></p>
  
  <pre><code>#include "MainWindow.h"
  
  MainWindow::MainWindow(QWidget *parent) : QWidget(parent)
  {
      boxLayout = new QHBoxLayout(this);
      startButton = new QPushButton("Start Counter", this);
      stopButton = new QPushButton("Stop Counter", this);
      lineEdit = new QLineEdit(this);
  
      boxLayout-&gt;addWidget(startButton);
      boxLayout-&gt;addWidget(stopButton); 
      boxLayout-&gt;addWidget(lineEdit);
  
      qDebug("Thread id %d",(int)QThread::currentThreadId());
  
      //When the start button is pressed, invoke the start() method in the counter thread
      QObject::connect(startButton,SIGNAL(clicked()),&amp;thread,SLOT(start()), Qt::QueuedConnection);
  
      //When the stop button is pressed, invoke the stop() method in the counter thread
      QObject::connect(stopButton,SIGNAL(clicked()),&amp;thread,SLOT(stopRunning()), Qt::QueuedConnection);
  
      //When the counter thread emits a signal saying its value has been updated, reflect that change in the lineEdit field.
      QObject::connect(&amp;thread,SIGNAL(signalValueUpdated(const QString&amp;)),lineEdit,SLOT(setText(const QString&amp;)), Qt::QueuedConnection);
  }
  </code></pre>