title: How to create QTcpServer on a QThread and then stop it from the main thread
body: <p>I have provided my entire code below. Please excuse the design as I am new to Qt and this is a quick and dirty example. </p>
  
  <p>What I want to do is create a QThread that manages a QTcpServer listener. I want it to be able to close listener when stopped and then reopen it when started again. Please ignore the fact that the listener may be getting leaked right now, I will tackle that issue later. </p>
  
  <p>I believe the problem is that when Stop is called it invokes the m_listener->close() method on a different thread (invoked from the main thread) than the thread it was created on (it was created on the TcpServerThread). So the questions is, how to I fix it? Or how to I redesign it so I avoid this issue?</p>
  
  <p>This isn't the full design, I have made a toy example that mimicks the real thing. </p>
  
  <p>tcpserverthread.h</p>
  
  <pre><code>#ifndef TCPSERVERTHREAD_H
  #define TCPSERVERTHREAD_H
  
  #include &lt;QObject&gt;
  #include &lt;QThread&gt;
  
  class QTcpServer;
  
  class TcpServerThread : public QThread
  {
      Q_OBJECT
  public:
      TcpServerThread();
  
      void run();
  
      void Start();
      void Stop();
  public slots:
      void newConnection();
  
  private:
  
      QTcpServer* m_pListener;
  };
  
  #endif // TCPSERVERTHREAD_H
  </code></pre>
  
  <p>tcpserverthread.cpp</p>
  
  <pre><code>#include "tcpserverthread.h"
  #include "tcpserver.h"
  #include &lt;iostream&gt;
  #include &lt;QTcpServer&gt;
  #include &lt;QTcpSocket&gt;
  #include &lt;QCoreApplication&gt;
  
  TcpServerThread::TcpServerThread()
  {
  
  }
  
  void TcpServerThread::run()
  {
      std::cout &lt;&lt; "thread started ..." &lt;&lt; std::endl;
      std::cout &lt;&lt; "listener starting ..." &lt;&lt; std::endl;
      m_pListener = new QTcpServer();
      connect(m_pListener, SIGNAL(newConnection()), this, SLOT(newConnection()));
      if(!m_pListener-&gt;listen(QHostAddress::Any, 1234))
      {
          std::cout &lt;&lt; "listener could NOT START - " &lt;&lt; m_pListener-&gt;errorString().toStdString() &lt;&lt; std::endl;
      }
      else
      {
          std::cout &lt;&lt; "listener SUCCESSFULLY STARTED" &lt;&lt; std::endl;
      }
  
  //    std::cout &lt;&lt; "thread running..." &lt;&lt; std::endl;
  //    m_pListener = new TcpServer();
  //    m_pListener-&gt;Start();
  
      exec();
  }
  
  void TcpServerThread::newConnection()
  {
      qDebug() &lt;&lt; "in new conn ... ";
      //std::cout &lt;&lt; "in new conn.." &lt;&lt; std::endl;
      QTcpSocket *soc = m_pListener-&gt;nextPendingConnection();
      soc-&gt;write("hello client");
  }
  
  void TcpServerThread::Start()
  {
      start();
  }
  
  void TcpServerThread::Stop()
  {
      std::cout &lt;&lt; "TcpServer Stopping . . . " &lt;&lt; std::endl;
      //this close is the line that causes the problem... 
      m_pListener-&gt;close();
      this-&gt;quit();
  }
  </code></pre>
  
  <p>main.cpp</p>
  
  <pre><code>#include &lt;QCoreApplication&gt;
  #include &lt;iostream&gt;
  #include &lt;QTcpServer&gt;
  #include "tcpserver.h"
  
  #include "tcpserverthread.h"
  
  int main(int argc, char *argv[])
  {
      QCoreApplication a(argc, argv);
  
      TcpServerThread *t = new TcpServerThread();
      t-&gt;Start();
      t-&gt;Stop();
  
      return a.exec();
  }
  </code></pre>
  
  <p>If you comment out the call to t->Stop in main you can then run it and use telnet (or putty) to connect to it @ 127.0.0.1:1234.</p>
  
  <p>I want to make it so it can be stopped and then started again and connections will work. </p>