QNetworkAccessManager 连接到 2 个回复槽,我怎么知道哪个回复属于哪个请求

QNetworkAccessManager connected to 2 reply slots, how do I know which reply belongs to which request

我的申请中有 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);

你的问题是两个插槽都连接到同一个信号,所以两者都会收到通知,即使你尝试断开不能保证其正常工作的信号,解决方案是连接信号每个 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();
}