QNetworkAccessManager连接到2个回复插槽,我如何知道哪个回复属于哪个请求

时间:2018-11-06 00:13:14

标签: c++ qt qt5 qnetworkrequest

我的应用程序中有1个QNetworkAccessManager,并且我同时发出2个请求。当我从经理那里得到答复时,答复的顺序与我给他们的顺序不符,这很有意义。我该如何解决?我是否应该由另一位经理清除所有请求队列问题?

QNetworkRequest request1(ONE_GET);
request1.setRawHeader("Content-Type", "application/vnd.api+json");
request1.setRawHeader("Accept", "application/vnd.api+json");  
m_nam.get(request1);

connect(&m_nam, &QNetworkAccessManager::finished,this , &HelperClass::onReply1Recieved);

QNetworkRequest request2(TWO_GET);
request2.setRawHeader("Content-Type", "application/vnd.api+json");
request2.setRawHeader("Accept", "application/vnd.api+json");

m_nam.get(request2);

connect(&m_nam, &QNetworkAccessManager::finished,this , &HelperClass::onReply2Recieved);

1 个答案:

答案 0 :(得分:2)

您所遇到的问题是两个插槽都连接到同一信号,因此将同时通知这两个插槽,即使您尝试断开无法保证其正常工作的信号,解决方案是连接每个插槽的信号。 QNetworkReply

QNetworkRequest request1(ONE_GET);
request1.setRawHeader("Content-Type", "application/vnd.api+json");
request1.setRawHeader("Accept", "application/vnd.api+json");  
QNetworkReply *reply1 = m_nam.get(request1);
connect(reply1, &QNetworkReply::finished, this, &HelperClass::onReply1Recieved);


QNetworkRequest request2(TWO_GET);
request2.setRawHeader("Content-Type", "application/vnd.api+json");
request2.setRawHeader("Accept", "application/vnd.api+json");
QNetworkReply *reply2 = m_nam.get(request2);
connect(reply2, &QNetworkReply::finished, this, &HelperClass::onReply2Recieved);

void HelperClass::onReply1Recieved(){
    QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
    qDebug() << reply->readAll();
}

void HelperClass::onReply2Recieved(){
    QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
    qDebug() << reply->readAll();
}