我有两个获得QNetworkRequest。
我想处理来自不同方法的完成信号。
例如,这是
中的代码 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*)));
}
这是我的一个方法
我尝试过重播 - > deleteLater();但结果相同
请给我一些有用的建议
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();
}
}
}
答案 0 :(得分:4)
您可以创建一个RequestSender
类,其角色正在处理请求。
每个RequestSender
对象将处理一个唯一请求。在创建将要发送的QNetworkRequest
时,RequestSender
将使用originatingObject
属性“标记”自己的请求。此属性指示发送请求的对象。当RequestSender
对象收到回复时,它将通过originatingObject
属性查看它是否是请求的发件人。有关originatingObject
的更多信息,请参阅此处的文档:http://qt-project.org/doc/qt-4.8/qnetworkrequest.html#originatingObject
以下是您可以做的一个示例。
requestsender.hpp:
class RequestSender {
public:
RequestSender();
~RequestSender();
void createRequest(/* Request parameters */);
public slots:
void endRequest(QNetworkReply* replay);
};
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
}
答案 1 :(得分:2)
使用QNetworkAccessManager执行的每个操作都将返回QNetworkReply。这也有一个信号finish()。也许您可以将此信号连接到不同的插槽。