Qt QNetworkAccessManager and multiple QNetworkReply
Date : March 29 2020, 07:55 AM
will be helpful for those in need You can create a RequestSender class whose role is looking after requests. Each RequestSender object will handle one unique request. While creating the QNetworkRequest that will be sent, the RequestSender will "tag" its own request with the originatingObject attribute. This attribute indicates which object sent the request. When a RequestSender object receives a reply, it will look if it is the sender of the request via the originatingObject attribute. For further informations about originatingObject, you can refer to the documentation here : http://qt-project.org/doc/qt-4.8/qnetworkrequest.html#originatingObjectclass RequestSender {
public:
RequestSender();
~RequestSender();
void createRequest(/* Request parameters */);
public slots:
void endRequest(QNetworkReply* replay);
};
RequestSender::RequestSender() {
connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(endRequest(QNetworkReply*)));
}
RequestSender::~RequestSender() {
disconnect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(endRequest(QNetworkReply*)));
}
void RequestSender::createRequest(/* Request parameters */) {
QNetworkRequest * myRequest = 0;
// Build myRequest with the request parameters
myRequest->setOriginatingObject(this);
nam->get(*myRequest);
}
void RequestSender::endRequest(QNetworkReply* replay) {
if (replay->request().originatingObject() != this) {
// That's not the request sent by the object -> stop the method here !
return;
}
// Treatments on replay
}
|
Qt QNetworkAccessManager & multiple QNetworkReplay
Tag : qt , By : Arun Thomas
Date : March 29 2020, 07:55 AM
Any of those help maybe you can do something like this: having an enum of the different methods enum GetMethod
{
getUserId,
getUserDetails
};
QHash<QNetworkReply*, GetMethod> hash;
QNetworkReply *reply1 = nam->post(requestUserId, data);
hash[reply1] = GetMethod::getUserId;
QNetworkReply *reply2 = nam->post(requestUserDetails, data);
hash[reply2] = GetMethod::getUserDetails;
connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(finished(QNetworkReply*)));
void MainWindow::finished(QNetworkReply *reply)
{
switch(hash[reply])
{
case GetMethod::getUserId:
GetUserIDCompleted(reply);
break;
case GetMethod::getUserDetails:
GetUserDetailsCompleted(reply);
break;
}
hash.remove(reply);
}
|
error: no matching function for call to 'QNetworkAccessManager::QNetworkAccessManager(Networking* const)'
Date : March 29 2020, 07:55 AM
I wish did fix the issue. This is my files. I have some problems with that. , Add Q_OBJECT macro to your class without ; class Networking : public QObject
{
Q_OBJECT
public:
Networking();
void getNetReply();
|
using QNetworkAccessManager GET multiple times
Tag : cpp , By : user161380
Date : March 29 2020, 07:55 AM
this one helps. I am writing an application to request a web page at equal intervals in order to get any changes in it (to check whether new data is received). here how i did it. , I see following problems:
|
trying to send file using QNetworkAccessManager get() but not QNetworkAccessManager post()
Date : March 29 2020, 07:55 AM
To fix the issue you can do I am a newbie, I am trying to send a .xml file to server using Qt4.8. My restriction is , I am told to use QNetworkAccessManager->get() only. Although QNetworkAccessManager->post is available but I can't use it as per instruction given to me. So, write following code:- , I figure it out, you have to add data as query. code will like this:- bool PIS::sendPISData(QString xmlFile,QString IP)
{
QFile xmlfyle(xmlFile);
if(!xmlfyle.open(QIODevice::ReadOnly))
{
#ifdef DEBUG
qDebug("Can not open file device.");
#endif
}
QString content = (QString)xmlfyle.readAll();
#ifdef DEBUG
qDebug()<<"content ::: "<<content;
#endif
xmlfyle.close();
QNetworkAccessManager mgr;
QEventLoop eventLoop;
QUrl url(QString("http://"+IP+"/Smart_Coach_Service/data_interchange/senddata"));
url.addQueryItem("xml_data", content);
QNetworkReply *replyn = mgr.get(reqpn);
connect(replyn, SIGNAL(finished()), &eventLoop, SLOT(quit()));
eventLoop.exec();
return true;
}
|