title: C++ thread affinity in QT error
body: <p>I have a simple Qt application that sends files from a folder to a server.</p>
  
  <p>When there is no internet connection, the expected behavior would be to save the file for later in an "offline" folder. Then, once a couple of minutes the application itself (in a different thread) would have to check that folder to see if some files await being sent. </p>
  
  <p>Here's some code, I stripped some unnecessary lines:</p>
  
  <p>main.cpp:</p>
  
  <pre><code>int main(int argc, char *argv[])
  {
      QApplication app(argc, argv);
  
      MainWin win;
      win.show();
  
      return app.exec();
  }
  </code></pre>
  
  <p>mainwin.cpp:</p>
  
  <pre><code>MainWin::MainWin(QWidget * parent) : QWebView(parent)
  {
  
      m_network = new QNetworkAccessManager(this);
  
      m_communicationHandler = new TestNetwork(this, m_network);
  
      // harvie
      Harvester * harvie = new Harvester(m_network);
      harvie-&gt;start();
  
  
  }
  </code></pre>
  
  <p>harvester.cpp:</p>
  
  <p>Header:</p>
  
  <pre><code>class Harvester : public QThread
  {
      Q_OBJECT
  public:
      QNetworkAccessManager * m_network;
      Harvester(QNetworkAccessManager * m_network);
  ...
  }
  </code></pre>
  
  <p>Source:</p>
  
  <pre><code>Harvester::Harvester(QNetworkAccessManager * m)
  {
      qDebug() &lt;&lt; "harvie start" &lt;&lt; endl;
      this-&gt;m_network = m;
  }
  </code></pre>
  
  <p>... </p>
  
  <pre><code>bool Harvester::sendFile(QFileInfo *fileInfo) {
  
      connect(this-&gt;m_network,SIGNAL(finished(QNetworkReply*)),this,SLOT(validateReply(QNetworkReply*)));
  
      QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
  
      QHttpPart filePart;
      filePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/xml")); // @todo test
      filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"someFile\"; filename=\"" + fileInfo-&gt;baseName() + ".xml\""));
  
      QFile *file = new QFile(fileInfo-&gt;filePath());
      file-&gt;open(QIODevice::ReadOnly);
      filePart.setBodyDevice(file);
      file-&gt;setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
  
  
      multiPart-&gt;append(filePart);
  
      qDebug() &lt;&lt; "post" &lt;&lt; endl;
  
      QNetworkReply *reply = this-&gt;m_network-&gt;post(QNetworkRequest(QUrl("XXX")), multiPart);
  
      multiPart-&gt;setParent(reply); // delete the multiPart with the reply
  
      return true;
  }
  </code></pre>
  
  <p>Here's the error I receive:</p>
  
  <pre><code>Warning: QObject: Cannot create children for a parent that is in a different thread.
  (Parent is QNetworkAccessManager(0x3e46c30), parent's thread is QThread(0x3dd6d50), current thread is Harvester(0x3e47eb8)
  </code></pre>
  
  <p>I guess the QNetworkAccessManager is conflicting when trying to send a request from another thread, even if I use the same pointer.</p>
  
  <p>One other solution that I did not tried yet would be to run harvester as a separate process, but first I'd like to solve it by threading.</p>
  
  <p>Thanks</p>