title: In a Qt console application, why is a QTimer required for the code to exit properly?
body: <p>I've wrote this code looking at examples online of how I'm supposed to run a console program that doesn't just run and quit and one that does. Based on a Qt console application. This one here, I wanted it to quit. I've understood pretty much everthing excepth the <code>QTimer::singleShot</code> line. If the line is commented out, the application will run but will not quit. If it is left, the application will run and quit as expected. Can anyone explain to me why?</p>
  
  <p>dostuff.h</p>
  
  <pre><code>#ifndef DOSTUFF_H
  #define DOSTUFF_H
  
  #include &lt;QObject&gt;
  #include &lt;iostream&gt;
  
  class DoStuff: public QObject
  {
      Q_OBJECT
  public :
      DoStuff(QObject *parent = 0);
  
  public slots:
      void run();
  
  signals:
      void finished();
  };
  
  #endif // DOSTUFF_H
  </code></pre>
  
  <p>And the implementation dostuff.cpp</p>
  
  <pre><code>#include "dostuff.h"
  
  DoStuff::DoStuff(QObject *parent):QObject(parent)
  {
  
  }
  
  void DoStuff::run(){
      for (int i = 0; i &lt; 10000; i++){
          std::cout &lt;&lt; "Processing " &lt;&lt; i &lt;&lt; std::endl;
      }
      emit(finished());
  }
  </code></pre>
  
  <p>My main.cpp</p>
  
  <pre><code>#include &lt;QCoreApplication&gt;
  #include &lt;QTimer&gt;
  #include "dostuff.h"
  
  int main(int argc, char *argv[])
  {
      QCoreApplication a(argc, argv);
  
      DoStuff *dostuff = new DoStuff(&amp;a);
      QObject::connect(dostuff,SIGNAL(finished()),&amp;a,SLOT(quit()));
      dostuff-&gt;run();
  
      // WHY THIS??
      QTimer::singleShot(10,dostuff,SLOT(run()));
  
      return a.exec();
  }
  </code></pre>