Hello
I have the following code:
Below is a Worker class which is QRunnable object. As you know, it is started from QThreadPool.
class Worker
: public QObject,
public QRunnable
{
Q_OBJECT
public:
Worker();
protected:
virtual void run();
};
class Worker : public QObject, public QRunnable
{
Q_OBJECT
public:
Worker();
protected:
virtual void run();
};
To copy to clipboard, switch view to plain text mode
void Worker::run()
{
Sender sender;
sender.send({"text1", "text2"});
}
void Worker::run()
{
Sender sender;
sender.send({"text1", "text2"});
}
To copy to clipboard, switch view to plain text mode
The sender.send() method:
void DataSender::send(const QList<QString>& data)
{
QNetworkRequest request(serviceUrl);
QJsonObject json;
for (auto& line : data)
json.insert("data", line);
QJsonDocument jsonDoc(json);
request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
request.
setHeader(QNetworkRequest
::ContentLengthHeader,
QByteArray::number(jsonData.
size()));
QNetworkReply *reply = _networkMgr.post(request, jsonData);
}
void DataSender::send(const QList<QString>& data)
{
QUrl serviceUrl = QUrl(given_ip_addr);
QNetworkRequest request(serviceUrl);
QJsonObject json;
for (auto& line : data)
json.insert("data", line);
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
Generally, the problem is: there is no any messages send to the endpoint given_ip_addr. No messages are received by the endpoint.
I think, the problem is in the running QNetworkAccessManager in the new thread, because I have tested another solution - I ran exactly the same send method in MAIN thread of whole application and there weren't any problems.
Do you have any idea? Unfortunately, I did not find anything in google about such problem.
Best Regards!
Bookmarks