我有两个http get方法。
一是获取用户id,二是获取当前用户的全部信息;
我要处理不同槽的成品标牌
处理getuserid finished with getuseridcompleted和处理getuserdetails with getuserdetailscompleted
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
nam = new QNetworkAccessManager(this);
GetUserID();
connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(GetUserIDCompleted(QNetworkReply*)));
GetUserDetails();
connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(GetUserDetailsCompleted(QNetworkReply*)));
}
是否可以在不同的插槽中获取qnetworkreplay?
最佳答案
也许您可以这样做:使用不同方法的枚举
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);
}
我没有试过,走了一些捷径,但你得到了它的精神。似乎您可以用答案检索请求,但我认为使用枚举更容易。
希望有帮助
关于qt - Qt QNetworkAccessManager和多个QNetworkReplay,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12090786/