title: Qt QTcpSocket with QtConcurrent::run Needs Event Loop in Separate Thread
body: <p>I have a web server in Qt that will read a very large ( ~1Gb ) file and return the data to the requestor through a QTcpSocket. This socket is created by the main server thread. I want to use QtConcurrent to hand off this socket to a worker thread and send the data back there. </p>
  
  <pre><code>// Using QtConcurrent
  BackgroundConcurrent childThreadToReturnLotsOfData;
  QFuture&lt;void&gt; futureObject = QtConcurrent::run(&amp;childThreadToReturnLotsOfData, &amp;BackgroundConcurrent::returnPartialLargeFile, resp , &amp;fileToCheckExistence);
  </code></pre>
  
  <p>My 'returnPartialLargeFile' function looks like:</p>
  
  <pre><code>void BackgroundConcurrent::returnPartialLargeFile( QHttpResponse *resp , QFile *fileToCheckExistence  ) const
  {
  
      // We need an event loop for the QTCPSocket slots
      QEventLoop loop;
      //QObject::connect(reply, SIGNAL(finished()), &amp;loop, SLOT(quit()));
      // Execute our event loop
      //loop.exec();
  
      // To do this in another thread from the one created, you must
      // move that socket to this thread, reparent and move
      resp-&gt;m_connection-&gt;m_socket-&gt;setParent( 0 );
      resp-&gt;m_connection-&gt;m_socket-&gt;moveToThread( QThread::currentThread() );
  
      // Read in chunks until we have sent all data back to the requestor
      QByteArray dataToWriteToSocket; // Store the data to send back
      while ( dataReadFromFileInBytes &gt; 0 ) {
  
          // Read some Data into the byte array
  
          // Write each chunk to the socket
          resp-&gt;write(dataToWriteToSocket); // &lt;----- Here is our data from the content of the file
          resp-&gt;flushData(); // &lt;----- Flush data to the socket
  
      }
  
      // End our response and close the connection
      resp-&gt;end();
      return;
  
  }
  </code></pre>
  
  <p>The error I get , is that if I leave the 'loop.exec()' line commented out, I get the following error:</p>
  
  <pre><code>ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread. Current thread c2630. Receiver '' (of type 'QTcpServer') was created in thread 910a8", file kernel/qcoreapplication.cpp, line 501
  </code></pre>
  
  <p>If I uncomment it, then my function here short circuits at the exec() line and never writes and data to the socket, but I do not get any errors, I just get a truncated response that does not include any data from the while loop.</p>
  
  <p>I am reparenting the socket and moving it to the new thread, so I hope my issues are only with the event loop and the socket signals and slots. Any ideas on what I am doing wrong here? How can I get this to work? If a signals/slots issue, which do I need to connect here? Thanks -</p>