PDA

View Full Version : why ONLY once???



timmu
24th August 2009, 15:27
My QNetworkRequest routine works fine and I get the info from the website that I want when I do this:



QNetworkAccessManager* nam = new QNetworkAccessManager(this);
QNetworkReply* reply; QUrl url; QString urlstring;
QObject::connect(nam, SIGNAL(finished(QNetworkReply*)),this, SLOT(finished(QNetworkReply*)));

//first
url.setUrl("http://www.google.com");
reply = nam->get(QNetworkRequest(url));


However, when I do it more than once, I always get the data for the latest request only:



QNetworkAccessManager* nam = new QNetworkAccessManager(this);
QNetworkReply* reply; QUrl url; QString urlstring;
QObject::connect(nam, SIGNAL(finished(QNetworkReply*)),this, SLOT(finished(QNetworkReply*)));

//first
url.setUrl("http://www.google.com");
reply = nam->get(QNetworkRequest(url));

//second
url.setUrl("http://www.yahoo.com");
reply = nam->get(QNetworkRequest(url));

//third
url.setUrl("http://www.hotmail.com");
reply = nam->get(QNetworkRequest(url));



Do you know why that is??

Here's my slot if that matters:



void MyClass::finished(QNetworkReply* reply)
{
QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute );
if (statusCodeV.toInt()==200){
//do something
}
else{
//do something
}

delete reply;
}


Thanks!

aamer4yu
25th August 2009, 05:29
From your code, you are re assigning the reply to latest replies. Hence its obvious.
If you want to process them all later, store them in different variables.

Also delete reply; in void MyClass::finished(QNetworkReply* reply) is not a good idea I guess. If you are passing something to a function, its good it doesnt delete. It will be hard to trace if any error comes. Am not sure about your design, just pointing if you really needed that.

timmu
25th August 2009, 08:00
Thanks, these were all excellent ideas.
Still, I'm not getting it to work.

Maybe someone knows answers to some of these questions:
1) If I should store all "reply" instances in different variables, then how do I automatically generate (within a while loop) generate these variables. When I just manually rename them reply1, reply2, etc. then I still get the same problem I got before.
2) How many times should I have the "connect..." and how many times should I declare "nam"? I think I'm missing something about how this signal/slot system really works.
3) Is it possible to make this website-probing independent of signals and slots altogether so that I could simply call the finished() function every time a new website is contacted?
4) Is it possible that I have a timing issue here? Maybe the program forces the websites to respond faster than they can? Maybe I should wait between different calls? Or is this why the signal/slot approaach is required for contacting webistes?
5) Does anyone know of good examples on the Internet about automatically contacting websites. This describes the process but it's not enough for me to figure it out http://doc.trolltech.com/4.4/qnetworkaccessmanager.html
http://wiki.forum.nokia.com/index.php/CS001431_-_Creating_an_HTTP_network_request_in_Qt



Thanks!