PDA

View Full Version : QNetworkAccessManager and multiple QNetworkReply



Acid
22nd August 2012, 18:02
Hello everybody.

I have two get QNetworkRequest.

I want to handle finished signals from different methods.

For example this is code in

GetUserData();
connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(GetUserDataCompleted(QNetworkReply*)));

GetMessages();
connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(GetMessagesCompleted(QNetworkReply*)));

my problem is that when GetMessages Request finished its calls both methods(GetUserDataCompleted, GetMessagesCompleted)

This my one method

I have tried replay->deleteLater(); but same result

Please advice me something useful

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();
}
}
}

StrikeByte
22nd August 2012, 18:48
If you connect both slots to one signal they always both get called
why do you want to handle the finished signal in two different slots?

yeye_olive
22nd August 2012, 23:52
When a QNetworkReply finishes, two signals are emitted:
1. the QNetworkAccessManager's finished() signal with a pointer to the QNetworkReply as its argument,
2. the QNetworkReply's finished() signal with no argument.

Clearly the first signal is the same for all QNetworkReplies and is therefore suitable for a uniform treatment such as updating a status bar, while the second signal is specific to the QNetworkReply instance and is therefore adapted to a reply-specific treatment, which is exactly what you are trying to achieve here.