问题描述
我有两个get QNetworkRequest.
I have two get QNetworkRequest.
我想处理来自不同方法的完成信号.
I want to handle finished signals from different methods.
例如,这是
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
GetUserData();
connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(GetUserDataCompleted(QNetworkReply*)));
GetMessages();
connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(GetMessagesCompleted(QNetworkReply*)));
}
这是我的一种方法
我尝试过replay-> deleteLater();但结果相同
I have tried replay->deleteLater(); but same result
请给我一些有用的建议
void MainWindow::GetUserDataCompleted(QNetworkReply *replay)
{
if(replay->error() == QNetworkReply::NoError)
{
QString getData = replay->readAll();
QMessageBox msg;
if(getData == "1")
{
msg.setText("User Is not Exits");
}
else
{
QDomDocument doc;
if(doc.setContent(getData))
{
QDomElement domElem = doc.documentElement();
QDomNode n = domElem.firstChild();
while(!n.isNull()) {
QDomElement e = n.toElement(); // try to convert the node to an element.
if(!e.isNull()) {
msg.setText(e.namedItem("Image").childNodes().at(0).nodeValue());
msg.exec();
}
n = n.nextSibling();
}
}
replay->deleteLater();
}
}
}
推荐答案
您可以创建一个RequestSender
类,该角色负责处理请求.
You can create a RequestSender
class whose role is looking after requests.
每个RequestSender
对象将处理一个唯一请求.创建要发送的QNetworkRequest
时,RequestSender
将使用originatingObject
属性标记"其自己的请求.此属性指示哪个对象发送了请求.当RequestSender
对象收到答复时,它将通过originatingObject
属性查看它是否是请求的发送者.有关originatingObject
的更多信息,您可以在此处参考文档: http ://qt-project.org/doc/qt-4.8/qnetworkrequest.html#originatingObject
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#originatingObject
下面是您可以做什么的示例.
Below is an example of what you can do.
requestsender.hpp:
requestsender.hpp :
class RequestSender {
public:
RequestSender();
~RequestSender();
void createRequest(/* Request parameters */);
public slots:
void endRequest(QNetworkReply* replay);
};
requestsender.cpp:
requestsender.cpp :
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和多个QNetworkReply的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!