Results 1 to 3 of 3

Thread: why ONLY once???

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2009
    Posts
    122
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    74

    Default why ONLY once???

    My QNetworkRequest routine works fine and I get the info from the website that I want when I do this:

    Qt Code:
    1. QNetworkAccessManager* nam = new QNetworkAccessManager(this);
    2. QNetworkReply* reply; QUrl url; QString urlstring;
    3. QObject::connect(nam, SIGNAL(finished(QNetworkReply*)),this, SLOT(finished(QNetworkReply*)));
    4.  
    5. //first
    6. url.setUrl("http://www.google.com");
    7. reply = nam->get(QNetworkRequest(url));
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. QNetworkAccessManager* nam = new QNetworkAccessManager(this);
    2. QNetworkReply* reply; QUrl url; QString urlstring;
    3. QObject::connect(nam, SIGNAL(finished(QNetworkReply*)),this, SLOT(finished(QNetworkReply*)));
    4.  
    5. //first
    6. url.setUrl("http://www.google.com");
    7. reply = nam->get(QNetworkRequest(url));
    8.  
    9. //second
    10. url.setUrl("http://www.yahoo.com");
    11. reply = nam->get(QNetworkRequest(url));
    12.  
    13. //third
    14. url.setUrl("http://www.hotmail.com");
    15. reply = nam->get(QNetworkRequest(url));
    To copy to clipboard, switch view to plain text mode 

    Do you know why that is??

    Here's my slot if that matters:

    Qt Code:
    1. void MyClass::finished(QNetworkReply* reply)
    2. {
    3. QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
    4. if (statusCodeV.toInt()==200){
    5. //do something
    6. }
    7. else{
    8. //do something
    9. }
    10.  
    11. delete reply;
    12. }
    To copy to clipboard, switch view to plain text mode 

    Thanks!
    Last edited by timmu; 24th August 2009 at 19:26.

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 334 Times in 317 Posts

    Default Re: why ONLY once???

    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.

  3. The following user says thank you to aamer4yu for this useful post:

    timmu (25th August 2009)

  4. #3
    Join Date
    Aug 2009
    Posts
    122
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    74

    Default Re: why ONLY once???

    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.ph..._request_in_Qt



    Thanks!
    Last edited by timmu; 25th August 2009 at 08:24.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.