Results 1 to 5 of 5

Thread: Tracking multiple requests with QNetwork

  1. #1
    Join Date
    May 2008
    Location
    Melbourne, Australia
    Posts
    136
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Tracking multiple requests with QNetwork

    hello,

    I have a class that communicates with a REST interface using the QNetwork stack.
    Here is some example code to make a HTTP request to the server that calls 'serverRequest()' when finished and 'requestError()' on error:

    Qt Code:
    1. QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    2. connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(serverRequest(QNetworkReply *)));
    3. QNetworkReply *reply = manager->get(QNetworkRequest(url));
    4. connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(requestError(QNetworkReply::NetworkError)));
    5.  
    6.  
    7. void serverRequest(QNetworkReply *r) {
    8. if(r->error() == QNetworkReply::NoError) {
    9. ...
    10. }
    11. delete r->manager();
    12. }
    13.  
    14. void requestError(QNetworkReply::NetworkError e) {
    15. switch(e) {
    16. case QNetworkReply::ConnectionRefusedError:
    17. ...
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

    My problem is that for certain errors (such as timeout but not 404) the error() signal is emitted but not finished().
    The error signal only passes an integer of the type of error so there is no direct way to tell which request failed. Also I can't clean up the memory used by the request.

    Currently I get around this with a QMap that stores when a request is made with a pointer to the QNetworkRequest instance. Then I use a QTimer to periodically retry unfinished requests.
    But this feels like a big hack. Is there a more elegant and flexible way to make network requests reliably?

    Richard

  2. #2
    Join Date
    May 2008
    Location
    Melbourne, Australia
    Posts
    136
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Tracking multiple requests with QNetwork

    does anyone know how to handle multiple network requests robustly with Qt?

  3. #3
    Join Date
    May 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Tracking multiple requests with QNetwork

    Hey,

    it's probably too late and I hope you found the answer by yourself, but for information, I think this->sender() will do the job.

  4. #4
    Join Date
    May 2008
    Location
    Melbourne, Australia
    Posts
    136
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Tracking multiple requests with QNetwork

    that project wrapped up some time ago, but thanks for the reference in future

  5. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Tracking multiple requests with QNetwork

    Rather than sender() and ugly type casting, your better off with QSignalMapper

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.