Results 1 to 8 of 8

Thread: QNetworkAccessManager::get() problem

  1. #1
    Join Date
    Jun 2008
    Posts
    64
    Thanks
    7
    Qt products
    Qt3 Qt4

    Default QNetworkAccessManager::get() problem

    http://www.qtcentre.org/threads/4025...send-s-nothing

    I am having a similar problem here.
    I used the QNetworkAccessManager::get()
    But from server side, I found no http request has been sent actually.


    Qt Code:
    1. void Report::test()
    2. {
    3. QNetworkAccessManager * manager = new QNetworkAccessManager();
    4. QNetworkConfigurationManager config_manager;
    5. manager->setConfiguration(config_manager.defaultConfiguration());
    6. connect( manager, SIGNAL( finished( QNetworkReply* ) ), this, SLOT( webReportGenerateFinished(QNetworkReply*) ));
    7. QNetworkReply *reply = manager->get(QNetworkRequest(QUrl("http://192.168.0.199/webreport/shift.php?title=ShiftReport&fromdate=2012-01-03&todate=2012-01-03&shift[]=2&emp[]=1&stationNo=1&lang=1&format=1&rounddp=1")));
    8. }
    9.  
    10. void Report::webReportGenerateFinished(QNetworkReply* reply)
    11. {
    12. cout<<"RM generate finished!"<<endl;
    13. qDebug("RM generate finished!");
    14. }
    To copy to clipboard, switch view to plain text mode 

    Header file:
    Qt Code:
    1. class Report : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public :
    6. ...
    7. ...
    8. ...
    9. ...
    10.  
    11. public slots:
    12. void webReportGenerateFinished(QNetworkReply * reply);
    To copy to clipboard, switch view to plain text mode 


    However, from http access log, NO request has been received.
    Would anyone please tell me what's going on?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QNetworkAccessManager::get() problem

    Are you returning to the Qt event loop after issuing your request?

    Is the finished slot being called? Have you looked at the error return available in the QNetworkReply? It might give you a clue.

  3. #3
    Join Date
    Jun 2008
    Posts
    64
    Thanks
    7
    Qt products
    Qt3 Qt4

    Default Re: QNetworkAccessManager::get() problem

    um...firstly I have not much idea for the event loop
    there's no error returned since I guess..... the request didn't send actually.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QNetworkAccessManager::get() problem

    So the slot is called but the error is QNewtorkReply::NoError or the slot is not called? These are not the same thing.

    The event loop in a typical Qt program is provided by the app.exec() call in your main() function. If there is no Qt event loop, or you are keeping the program from returning to it (e.g. by busy waiting or calling some blocking process) then the network request will not be sent and your GUI will be unresponsive. For example, if you did this:
    Qt Code:
    1. Report rep;
    2. ...
    3. rep.test();
    4. while (the result of the finished request is not present)
    5. waitAWhile();
    To copy to clipboard, switch view to plain text mode 
    then your code will never leave the while loop because the request cannot be sent until control returns to the event loop (i.e. your program becomes idle).

    Put together a small, self-contained example program that displays the problem you are seeing.

  5. The following user says thank you to ChrisW67 for this useful post:

    batileon (16th January 2012)

  6. #5
    Join Date
    Jun 2008
    Posts
    64
    Thanks
    7
    Qt products
    Qt3 Qt4

    Default Re: QNetworkAccessManager::get() problem

    Thanks!

    The case is actually slot not called, and from server I found the http request not even being sent.

    I tried to add a few lines involving QEventLoop, after calling the exec(), it worked!

    But I am still figuring how to use the eventloop since .....it holds the program, I am finding way to end it........

  7. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QNetworkAccessManager::get() problem

    Call QEventLoop::quit() or exit(). However, you should reconsider the design that is causing the program to block and not return to the main Qt event loop in the first place.

  8. #7
    Join Date
    Jun 2008
    Posts
    64
    Thanks
    7
    Qt products
    Qt3 Qt4

    Default Re: QNetworkAccessManager::get() problem

    I am a bit confused.
    How can I call the quit() or exit without the object? Where to quit? Inside the slot?

    What I have now is something like:
    Qt Code:
    1. void Report::test()
    2. {
    3.  
    4. QNetworkAccessManager * manager = new QNetworkAccessManager();
    5. QNetworkConfigurationManager config_manager;
    6. manager->setConfiguration(config_manager.defaultConfiguration());
    7. connect( manager, SIGNAL( finished( QNetworkReply* ) ), this, SLOT( webReportGenerateFinished(QNetworkReply*) ));
    8. QNetworkReply *reply = manager->get(QNetworkRequest(QUrl("http://192.168.0.199/webreport/shift.php?title=ShiftReport&fromdate=2012-01-03&todate=2012-01-03&shift[]=2&emp[]=1&stationNo=1&lang=1&format=1&rounddp=1")));
    9.  
    10. loop.exec()
    11. }
    12.  
    13. void Report::webReportGenerateFinished(QNetworkReply* reply)
    14. {
    15. cout<<"RM generate finished!"<<endl;
    16. ..
    17. ..
    18. ..// call loop.quit() here??? Do not have the loop object actually....
    19.  
    20. qDebug("RM generate finished!");
    21. }
    To copy to clipboard, switch view to plain text mode 

  9. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QNetworkAccessManager::get() problem

    QEventLoop::quit() is a slot... connect something to it, or change the scope of the QEventLoop.

    Better still, identify the flaw in your original design. What are you doing after you call test() that is not allowing the normal flow of events?

  10. The following user says thank you to ChrisW67 for this useful post:

    batileon (17th January 2012)

Similar Threads

  1. Replies: 2
    Last Post: 14th September 2019, 18:43
  2. Replies: 14
    Last Post: 28th August 2011, 14:11
  3. Replies: 8
    Last Post: 19th April 2011, 07:17
  4. QNetworkAccessManager problem instantiating
    By hojoff79 in forum Qt Programming
    Replies: 8
    Last Post: 9th February 2011, 04:59
  5. qnetworkaccessmanager problem!
    By novamaster in forum Qt Programming
    Replies: 6
    Last Post: 7th August 2010, 11:46

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.