Hello,

I have the such code architecture. For each request I am creating Worker class, which is QRunnable object and it should be started in a new QThread by QThreadPool.
This Worker has the following code:

Qt Code:
  1. #include "Worker.h"
  2. #include <QString>
  3.  
  4. Worker::Worker(const WorkerData& data) :
  5. _reader()
  6. _writer(),
  7. _sender()
  8. {
  9. }
  10.  
  11. void Worker::run()
  12. {
  13. _reader.readFile();
  14. }
  15.  
  16. void Worker::sendData(const DataToSend& data)
  17. {
  18. _writer.write(data);
  19. _sender.send(data);
  20. }
To copy to clipboard, switch view to plain text mode 

The Sender header code:
Qt Code:
  1. #include <QObject>
  2. #include <QtNetwork/QNetworkAccessManager>
  3.  
  4. class DataSender : public QObject
  5. {
  6. Q_OBJECT
  7. public:
  8. DataSender();
  9.  
  10. void send(const DataToSend& data);
  11.  
  12. private:
  13. QNetworkAccessManager _networkMgr;
  14. };
To copy to clipboard, switch view to plain text mode 

The Sender.cpp code:
Qt Code:
  1. #include <QUrl>
  2. #include <QJsonObject>
  3. #include <QJsonDocument>
  4. #include <QtNetwork/QNetworkReply>
  5. DataSender::DataSender() : _networkMgr()
  6. {
  7.  
  8. }
  9.  
  10. void DataSender::send(const DataToSend& data)
  11. {
  12. qDebug() << "\tINFO: [DataSender::send] sending data to endpoint: " << data;
  13.  
  14. QUrl serviceUrl = QUrl("http://someUrl.com");
  15. QNetworkRequest request(serviceUrl);
  16. QJsonObject json;
  17. json.insert("data", data.first());
  18. QJsonDocument jsonDoc(json);
  19. QByteArray jsonData= jsonDoc.toJson();
  20. request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
  21. request.setHeader(QNetworkRequest::ContentLengthHeader,QByteArray::number(jsonData.size()));
  22. QNetworkReply *reply = _networkMgr.post(request, jsonData);
  23. }
To copy to clipboard, switch view to plain text mode 

As you know, the only run() method in QRunnable object is running in a new thread. Based on that, all my QNetworkAccessManager is created in the old thread, because it is not created in run method, am I right?

I am asking about that, because i got the following error:

QObject: Cannot create children for a parent that is in a different thread.
(Parent is QNetworkAccessManager(0xda5a74c), parent's thread is QThread(0xdb14e0), current thread is QThread(0xda579a0)
Do you have any idea why I get this error?
I do not see the cause of the problem...