Results 1 to 18 of 18

Thread: QNetworkReply: how to know when data is sent?

  1. #1
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default QNetworkReply: how to know when data is sent?

    I am using QNetworkAccessManager:post method for sending data to a server. Waiting for reply may take a long, but I don't really need it: I just need to know when the data is sent. Is it possible?

    For example, I need to wait until logout command is sent before destroying QNetworkAccessManager.
    Last edited by mentalmushroom; 23rd June 2011 at 07:42. Reason: smiles disabled

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QNetworkReply: how to know when data is sent?

    You can send a logout command in a slot connected to this signal
    Qt Code:
    1. void QNetworkAccessManager::finished ( QNetworkReply * reply ) ;
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: QNetworkReply: how to know when data is sent?

    Are you sure it will be enough time to send all the data before QNetworkAccessManager is destroyed? Perhaps I need to delete it right after exiting from the slot connected to QNetworkAccessManager::finished.
    What if I don't even have anything connected to finished signal? Perhaps I simply need to send data and quit, something like this:
    Qt Code:
    1. QNetworkAccessManager manager;
    2. manager.post(...);
    3. // need to wait until data is sent here
    4. return;
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QNetworkReply: how to know when data is sent?

    QNetworkAccessManager API doesn't allow you to trace when it is finished posting data. Either wait for the response to come or don't use QNAM.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: QNetworkReply: how to know when data is sent?

    Ok, what could be a good alternative for that purpose?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QNetworkReply: how to know when data is sent?

    QHttp should work fine. It has a dataSendProgress() signal.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: QNetworkReply: how to know when data is sent?

    Ok, i thought to use it from the very beginning, I didn't just because I read the following in the Qt documentation: "This class is obsolete. It is provided to keep old source code working. We strongly advise against using it in new code."

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QNetworkReply: how to know when data is sent?

    Yes, that's true but your requirement is somewhat different from what people use HTTP for in their apps. I don't know why you don't want to wait for the server's response -- you might get a 404 or 500 or some other fault. I think it's worth knowing if your request went to /dev/null or not.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: QNetworkReply: how to know when data is sent?

    I may want to upload some data or send logout. Sometimes it takes a several seconds (depending on connection, network problems etc). In my application it is more important to proceed quickly than know whether the data was uploaded or not. I don't care much how successful was logout sent to some third party server, but I want my app to be closed quickly, so users won't need to wait until slow server responds.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QNetworkReply: how to know when data is sent?

    So why do you want to send the logout at all if you don't care if it works or not?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: QNetworkReply: how to know when data is sent?

    I send logout because it is better to send than not to send (third party server recommends to send it). Also I upload some data that may be useful, but if it fails it is not critical. Moreover if logout fails in most cases I can't do anything with that, so result can be only logged somewhere and it is not that important for users. But as long as it doesn't make my application inconvenient to users I think it is better to call logout and upload all the data that may be useful.

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QNetworkReply: how to know when data is sent?

    Then don't wait for anything, send the data and start closing your app. With a bit of luck, the request will go through. If not then you don't care anyway... If you do care, then wait a predefined amount of time for a response and if you don't get it during this period, abandon the request and close your app.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: QNetworkReply: how to know when data is sent?

    Well, eventually I came to the same approach. I can afford it to wait for let's say 1 second, the only bad side is that waiting for data sent in most cases would take less time, but with this approach I still have to wait 1 second that is for the worst case. My question: is there any place where I can ask to add this feature? To my mind having dataSent signal would be useful.

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QNetworkReply: how to know when data is sent?

    Qt is open-source, add the feature yourself. It should be just a couple lines of code.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. #15
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QNetworkReply: how to know when data is sent?

    You can delete QNetworkAccessManager, when things are done, don't need to wait, only thing is create QNetworkAccessManager on heap, here is a snippet

    Qt Code:
    1. MyObject::sendLastPost()
    2. {
    3. QNetworkAccessManager * manager = new QNetworkAccessManager;
    4. connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(logout()));
    5. post(request, data); //Send Data, for which reply is not cared for, and also ignore post fails
    6. }
    7.  
    8. void MyObject::logout(void) //slot, don't call direclty
    9. {
    10. QNetworkAccessManager * manager = dynamic_cast<QNetworkAccessManager *>(sender());
    11. if(manager)
    12. {
    13. disconnect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(logout()));
    14. connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(deleteLater()));
    15. post(request, data); //Send Logout data
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    Just call sendLastPost(), rest should be taken care, including deletion.

  16. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QNetworkReply: how to know when data is sent?

    He wants to quit the app immediately so this won't do. And furthermore some servers will abandon the request if the remote site terminates connection so I think all this approach of posting data without waiting for response has little chance of working.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  17. #17
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QNetworkReply: how to know when data is sent?

    Ok, I missed the Application exit requirement

    Quote Originally Posted by mentalmushroom
    I send logout because it is better to send than not to send (third party server recommends to send it). Also I upload some data that may be useful, but if it fails it is not critical. Moreover if logout fails in most cases I can't do anything with that, so result can be only logged somewhere and it is not that important for users. But as long as it doesn't make my application inconvenient to users I think it is better to call logout and upload all the data that may be useful.
    Looks like the kind of closing sequence

    1. App wants to exit
    2. then log into server
    3. upload usage data (any useful, but not critical data)
    4. logout
    5. exit app

    OP can also connect QNetworkAccessManage::finished() to QCoreApplication::quit (), then hide() the MainWindow (all the MainWindows), this should not make application inconvenient to users

  18. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QNetworkReply: how to know when data is sent?

    It wouldn't be inconvenient for me if the window was there saying it uploads data I prefer that to a hidden window with the application looking like frozen. But then everyone has his own inconveniences...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 1
    Last Post: 8th March 2011, 06:27
  2. Subclassing QNetworkReply
    By piotr.dobrogost in forum Qt Programming
    Replies: 6
    Last Post: 19th December 2010, 08:42
  3. Cannot get QNetworkCookie from QNetworkReply
    By MorrisLiang in forum Newbie
    Replies: 1
    Last Post: 25th May 2010, 16:59
  4. QNetworkReply::HostNotFoundError
    By timmu in forum Qt Programming
    Replies: 1
    Last Post: 25th August 2009, 10:58
  5. When to delete QNetworkReply?
    By QPlace in forum Qt Programming
    Replies: 5
    Last Post: 11th February 2009, 12:46

Tags for this Thread

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.