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:
#include "Worker.h"
#include <QString>
Worker::Worker(const WorkerData& data) :
_reader()
_writer(),
_sender()
{
}
void Worker::run()
{
_reader.readFile();
}
void Worker::sendData(const DataToSend& data)
{
_writer.write(data);
_sender.send(data);
}
#include "Worker.h"
#include <QString>
Worker::Worker(const WorkerData& data) :
_reader()
_writer(),
_sender()
{
}
void Worker::run()
{
_reader.readFile();
}
void Worker::sendData(const DataToSend& data)
{
_writer.write(data);
_sender.send(data);
}
To copy to clipboard, switch view to plain text mode
The Sender header code:
#include <QObject>
#include <QtNetwork/QNetworkAccessManager>
{
Q_OBJECT
public:
DataSender();
void send(const DataToSend& data);
private:
QNetworkAccessManager _networkMgr;
};
#include <QObject>
#include <QtNetwork/QNetworkAccessManager>
class DataSender : public QObject
{
Q_OBJECT
public:
DataSender();
void send(const DataToSend& data);
private:
QNetworkAccessManager _networkMgr;
};
To copy to clipboard, switch view to plain text mode
The Sender.cpp code:
#include <QUrl>
#include <QJsonObject>
#include <QJsonDocument>
#include <QtNetwork/QNetworkReply>
DataSender::DataSender() : _networkMgr()
{
}
void DataSender::send(const DataToSend& data)
{
qDebug() << "\tINFO: [DataSender::send] sending data to endpoint: " << data;
QUrl serviceUrl
= QUrl("http://someUrl.com");
QNetworkRequest request(serviceUrl);
QJsonObject json;
json.insert("data", data.first());
QJsonDocument jsonDoc(json);
request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
request.
setHeader(QNetworkRequest
::ContentLengthHeader,
QByteArray::number(jsonData.
size()));
QNetworkReply *reply = _networkMgr.post(request, jsonData);
}
#include <QUrl>
#include <QJsonObject>
#include <QJsonDocument>
#include <QtNetwork/QNetworkReply>
DataSender::DataSender() : _networkMgr()
{
}
void DataSender::send(const DataToSend& data)
{
qDebug() << "\tINFO: [DataSender::send] sending data to endpoint: " << data;
QUrl serviceUrl = QUrl("http://someUrl.com");
QNetworkRequest request(serviceUrl);
QJsonObject json;
json.insert("data", data.first());
QJsonDocument jsonDoc(json);
QByteArray jsonData= jsonDoc.toJson();
request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
request.setHeader(QNetworkRequest::ContentLengthHeader,QByteArray::number(jsonData.size()));
QNetworkReply *reply = _networkMgr.post(request, jsonData);
}
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...
Bookmarks