title: Qt signals and slots, threads, app.exec(), and related queries
body: <p>[related to <a href="https://stackoverflow.com/questions/1450500">this question</a>]</p>
  <p>I wrote this piece of code to understand how qt signals and slots work. I need someone to explain the behaviour, and to tell me if I'm right about my own conclusions.</p>
  <p>My program:</p>
  <p><code>connectionhandler.h</code></p>
  <pre><code>#ifndef CONNECTIONHANDLER_H
  #define CONNECTIONHANDLER_H
  
  #include &lt;QTcpServer&gt;
  class ConnectionHandler : public QObject
  {
      Q_OBJECT
  public:
      ConnectionHandler();
  public slots:
      void newConn();
  private:
      QTcpServer *server;
  };
  
  #endif // CONNECTIONHANDLER_H
  </code></pre>
  <p><code>connectionhandler.cpp</code></p>
  <pre><code>#include &quot;connectionhandler.h&quot;
  #include &lt;QTextStream&gt;
  
  ConnectionHandler::ConnectionHandler() {
      server = new QTcpServer;
      server-&gt;listen(QHostAddress::LocalHost, 8080);
      QObject::connect(server, SIGNAL(newConnection()),this, SLOT(newConn()));
  }
  void ConnectionHandler::newConn() {
      QTextStream out(stdout);
      out &lt;&lt; &quot;new kanneksan!\n&quot;;
      out.flush();
  }
  </code></pre>
  <p><code>main.cpp</code></p>
  <pre><code>#include &lt;QCoreApplication&gt;
  #include &quot;connectionhandler.h&quot;
  
  int main(int argc, char* argv[]) {
      QCoreApplication app(argc,argv);
      ConnectionHandler handler;
      return app.exec();
  }
  </code></pre>
  <p>Now, running this program sends it into an infinite loop looking for new connections.</p>
  <p><code>Observation:</code> if I don't call <code>app.exec()</code>, the program returns immediately (as it should).<br />
  <code>Question:</code> why?</p>
  <p><code>Question:</code> if I had connected the slot as a queued connection, when would the slot invocation be performed?<br />
  <code>Question:</code> if <code>app.exec()</code> is an infinite loop of sorts, how does the <code>newConnection()</code> signal ever get emitted?</p>
  <p><code>Big Question:</code> Is their any &quot;second thread&quot; involved here? (I expect a no, and a stunningly elegant explanation :) )</p>
  <p>Thanks,<br />
  jrh</p>
  <p>PS: who else has this nested parenthesis syndrome? like &quot;(.. :))&quot; or &quot;(.. (..))&quot;?</p>