title: Qt - Handle QTcpSocket in a new thread
body: <p>Trying to handle a connected client socket in a new thread from global thread pool:</p>
  
  <pre><code>m_threadPool = QThreadPool::globalInstance();
  
  void TCPListenerThread::onNewConnection()
  {
      QTcpSocket *clientSocket = m_tcpServer-&gt;nextPendingConnection();
      clientSocket-&gt;localPort();
      m_connectThread = new TCPConnectThread(clientSocket);
      m_threadPool-&gt;start(m_connectThread);
  }
  </code></pre>
  
  <p>Here's <code>TCPConnectThread</code>:  </p>
  
  <pre><code>class TCPConnectThread : public QRunnable {
      TCPConnectThread::TCPConnectThread(QTcpSocket *_socket)
      {
          m_socket = _socket;
          this-&gt;setAutoDelete(false);
      }
  
  
      void TCPConnectThread::run()
      {
          if (! m_socket-&gt;waitForConnected(-1) )
              qDebug("Failed to connect to client");
          else
              qDebug("Connected to %s:%d %s:%d", m_socket-&gt;localAddress(), m_socket-&gt;localPort(), m_socket-&gt;peerAddress(), m_socket-&gt;peerPort());
  
          if (! m_socket-&gt;waitForReadyRead(-1))
              qDebug("Failed to receive message from client") ;
          else
              qDebug("Read from client: %s",   QString(m_socket-&gt;readAll()).toStdString().c_str());
  
          if (! m_socket-&gt;waitForDisconnected(-1))
              qDebug("Failed to receive disconnect message from client");
          else
              qDebug("Disconnected from client");
      }
  }
  </code></pre>
  
  <p>I have been getting endless errors with these. It seems cross-thread <code>QTcpSocket</code> handling is <a href="https://stackoverflow.com/questions/2698145/how-do-i-execute-qtcpsocket-in-a-different-thread">not feasible</a>(See Michael's answer).  </p>
  
  <p>Some errors:  </p>
  
  <pre><code>QSocketNotifier: socket notifiers cannot be disabled from another thread  
  
  ASSERT failure in QCoreApplication::sendEvent: "Cannot send events t objects owned by a different thread.  
  </code></pre>
  
  <p>Should I handle <code>QTcpSocket</code> in a different thread ?<br>
  What should I do if I want to handle <code>QTcpSocket</code> in a different thread ?<br>
  Or is there way to create a <code>QTcpSocket</code> from a file descriptor ?  </p>