Results 1 to 7 of 7

Thread: http request without signals and slots

  1. #1
    Join Date
    Jan 2010
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default http request without signals and slots

    hi all.
    i'm a newbie in qt programmin, so please be patient.
    i'm writing a qt library and i need to perform an http request to a remote server and, of course, read the response.
    i don't need at all to use the signals and slots system, because i have to wait for the http call to finish before going on with the code.
    is there a way to do that? i prefer to use the new network api, if possible

    thanks

    christian

  2. #2
    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: http request without signals and slots

    Use QNetworkAccessManager together with QEventLoop.
    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.


  3. #3
    Join Date
    Jan 2010
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: http request without signals and slots

    thanks for the answer.

    my "download" method is this (code found on the forum, i think it's posted by you):

    Qt Code:
    1. QNetworkAccessManager* manager = new QNetworkAccessManager();
    2. QUrl murl = this->url;
    3. QNetworkReply *reply = manager->get(QNetworkRequest(url));
    4. QEventLoop loop;
    5. connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
    6. QTimer timer;
    7. connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit())); // just in case
    8. timer.start(5000);
    9. loop.exec();
    10. QByteArray result = reply->readAll();
    11.  
    12. if ( reply->error() != QNetworkReply::NoError ) {
    13. qDebug() << "Request failed:" << reply->errorString();
    14. return;
    15. }
    16.  
    17. qDebug() << "Reply from the server: ";
    18. qDebug() << result;
    19. }
    To copy to clipboard, switch view to plain text mode 

    but when i run it i get:

    QEventLoop: Cannot be used without QApplication
    QObject::startTimer: QTimer can only be used with threads started with QThread
    and the program loops

    i'm writing a shared library, so there is not a QApplication. is there another way, then?

  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: http request without signals and slots

    No, you need a Q(Core)Application object. Someone has to create it - either you when initializing your library or the application that uses your library.
    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
    Jan 2010
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: http request without signals and slots

    thanks, declaring a QCoreApplication did the trick.
    last quetion, is there a way to esatablish if a QCoreApplication exists before creating a new one? in that case i could create a qcoreapplication only if needed

  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: http request without signals and slots

    QCoreApplication is a singleton, you can query for its instance through QCoreApplication::instance().
    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
    Jan 2010
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: http request without signals and slots

    well wysota, thanks again
    you have a beer payed if you pass here

Similar Threads

  1. HTTP request
    By Trok in forum Qt Programming
    Replies: 9
    Last Post: 5th January 2010, 14:49
  2. problem with Synchronous Http request
    By rchaitanya in forum Qt for Embedded and Mobile
    Replies: 2
    Last Post: 30th January 2009, 11:03
  3. How do I send data with http post request?
    By Morea in forum Qt Programming
    Replies: 13
    Last Post: 21st January 2009, 22:51
  4. http request
    By yagabey in forum Qt Programming
    Replies: 3
    Last Post: 28th December 2008, 19:19
  5. Signals and Slots
    By 83.manish in forum Qt Programming
    Replies: 3
    Last Post: 30th June 2008, 10:31

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.