title: How to do proper threading in QT GUI application?
body: <p>I want to run two threads concurrently. I successfully did the same when I ran a program as a QT console application.
  Here's the working code of QT console application for multi threading:-</p>
  
  <p>myobject.h</p>
  
  <pre><code>#ifndef MYOBJECT_H
  #define MYOBJECT_H
  
  #include &lt;QObject&gt;
  #include &lt;QDebug&gt;
  #include &lt;QThread&gt;
  
  class MyObject : public QObject
  {
      Q_OBJECT
  public:
      explicit MyObject(QObject *parent = 0);
      void doSetup(QThread &amp;cThread);
      void doSetup2(QThread &amp;cThread2);
  signals:
  
  public slots:
      void doWork();
      void doWork2();
  };
  
  #endif // MYOBJECT_H
  </code></pre>
  
  <p>main.cpp:</p>
  
  <pre><code>#include &lt;QtCore/QCoreApplication&gt;
  #include &lt;QThread&gt;
  #include &lt;myobject.h&gt;
  int main(int argc, char *argv[])
  {
      QCoreApplication a(argc, argv);
  
      QThread cThread, cThread2;
      MyObject cObject, cObject2;
  
      cObject.doSetup(cThread);
      cObject.moveToThread(&amp;cThread);
      cObject2.doSetup2(cThread2);
      cObject2.moveToThread(&amp;cThread2);
      cThread.start();
      cThread2.start();
      qDebug()&lt;&lt;"hello there ";
      return a.exec();
  }
  </code></pre>
  
  <p>my object.cpp</p>
  
  <pre><code>#include "myobject.h"
  #include &lt;QThread&gt;
  #include "tftpServer.h"
  
  MyObject::MyObject(QObject *parent) :
      QObject(parent)
  {
  }
  
  void MyObject::doSetup(QThread &amp;cThread)
  {
      connect(&amp;cThread, SIGNAL(started()), this, SLOT(doWork()));
  }
  
  void MyObject::doSetup2(QThread &amp;cThread2)
  {
      connect(&amp;cThread2, SIGNAL(started()), this, SLOT(doWork2()));
  }
  void MyObject::doWork()
  {
      for(int i=0; i&lt;1000; i++)
      {
          qDebug()&lt;&lt;"******************Thread 1";
  
  
      }
  
  }
  void MyObject::doWork2()
  {
      for(int i=1000; i&lt;2000; i++)
      {
          qDebug()&lt;&lt;"Thread 2************************";
  
  
      }
  
  }
  </code></pre>
  
  <p>Here's the output:</p>
  
  <pre><code>******************Thread 1
  Thread 2************************
  ******************Thread 1
  Thread 2************************
  ******************Thread 1
  Thread 2************************
  ******************Thread 1
  Thread 2************************
  </code></pre>
  
  <p>..and so on</p>
  
  <p>Now when I use this almost same code and run as a QT GUI application, the threads do not run concurrently, but run one after the other. Here's the code:</p>
  
  <p>mainwindow.h</p>
  
  <pre><code>#ifndef MAINWINDOW_H
  #define MAINWINDOW_H
  
  #include &lt;QMainWindow&gt;
  #include &lt;QDebug&gt;
  #include &lt;QThread&gt;
  
  namespace Ui {
  class MainWindow;
  }
  
  class MainWindow : public QMainWindow
  {
      Q_OBJECT
  
  public:
      explicit MainWindow(QWidget *parent = 0);
      ~MainWindow();
      void doSetup(QThread &amp;cThread);
      void doSetup2(QThread &amp;cThread2);
  private:
      Ui::MainWindow *ui;
  
  public slots:
      void doWork();
      void doWork2();
  };
  
  #endif // MAINWINDOW_H
  </code></pre>
  
  <p>main.cpp</p>
  
  <pre><code>#include &lt;QtGui/QApplication&gt;
  #include "mainwindow.h"
  #include &lt;QThread&gt;
  int main(int argc, char *argv[])
  {
      QApplication a(argc, argv);
  
      QThread cThread, cThread2;
      MainWindow cObject, cObject2;
  
      cObject.doSetup(cThread);
      cObject.moveToThread(&amp;cThread);
      cObject2.doSetup2(cThread2);
      cObject2.moveToThread(&amp;cThread2);
      cThread.start();
      cThread2.start();
  
      MainWindow w;
      w.show();
  
      return a.exec();
  }
  </code></pre>
  
  <p>mainwindow.cpp</p>
  
  <pre><code>#include "mainwindow.h"
  #include "ui_mainwindow.h"
  #include &lt;QThread&gt;
  #include &lt;qthread.h&gt;
  
  MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
  {
      ui-&gt;setupUi(this);
      qDebug()&lt;&lt;"gui running";
  }
  
  MainWindow::~MainWindow()
  {
      delete ui;
  }
  
  void MainWindow::doSetup(QThread &amp;cThread)
  {
      connect(&amp;cThread, SIGNAL(started()), this, SLOT(doWork()));
  }
  
  void MainWindow::doSetup2(QThread &amp;cThread2)
  {
      connect(&amp;cThread2, SIGNAL(started()), this, SLOT(doWork2()));
  }
  void MainWindow::doWork()
  {
      //QThread::sleep(100);
      for(int i=0; i&lt;1000; i++)
      {
        qDebug()&lt;&lt;"******************Thread 1";
  //      qDebug()&lt;&lt;i;
  
      }
  
  }
  void MainWindow::doWork2()
  {
      for(int i=1000; i&lt;2000; i++)
      {
        qDebug()&lt;&lt;"Thread 2************************";
  //        qDebug()&lt;&lt;i;
  
      }
  
  }
  </code></pre>
  
  <p>Here's the output:</p>
  
  <pre><code>gui running 
  gui running 
  QObject::moveToThread: Widgets cannot be moved to a new thread
  QObject::moveToThread: Widgets cannot be moved to a new thread
  gui running 
  ******************Thread 1 
  ******************Thread 1 
  ******************Thread 1 
  ******************Thread 1
  </code></pre>
  
  <p>(and so on...)</p>
  
  <pre><code>******************Thread 1
  Thread 2************************ 
  Thread 2************************ 
  Thread 2************************ 
  Thread 2************************ 
  Thread 2************************ 
  Thread 2************************ 
  </code></pre>
  
  <p>(and so on...)
  Note that the only difference b/w console and gui application I made is changed the base class QObject to QMainWindow, and hence the thread objects so created are objects of QObject &amp; QMainWindow respectively, for console and GUI applications.</p>
  
  <p>Hoping I am sufficiently clear, can you please tell me what mistake am I committing, which is making the threads run one after the other and not concurrently, as it does for the console application?</p>
  
  <p>Thank you.</p>