Home > howto, programming > Qt: (Nearly) synchronous QNetworkAccessManager calls

Qt: (Nearly) synchronous QNetworkAccessManager calls

The QNetworkAccessManager class is very user-friendly, but it makes asynchronous calls. I was in the need for synchronous calls to handle my HTTP communication, but I did not want the overhead of another thread, so I googled a bit and finally came up with a short call to an event loop that processed the request. Like this:

QNetworkAccessManager * pnam = new QNetworkAccessManager(this);
// the slot was declared at another place
connect(pnam, SIGNAL(finished(QNetworkReply *)), this,
  SLOT(loginFinished(QNetworkReply*)));
QNetworkRequest req(QUrl("http://foo.bar"));
pnam->post(req, postData);

// execute an event loop to process the request (nearly-synchronous)
QEventLoop eventLoop;
// also dispose the event loop after the reply has arrived
connect(pnam, SIGNAL(finished(QNetworkReply *)), &eventLoop, SLOT(quit()));
eventLoop.exec();

This way my user-defined slot for the pnam->finished() signal was called immediately, and I could be sure to have the HTTP reply at the end of this code snippet.

Found here: Qt-Interest Mailing List: QNetworkAccessManager and QNetworkReply, synchronous


Flattr this

Advertisements
  1. balta2ar
    November 8, 2010 at 15:00

    You probably meant:

    connect(&req, SIGNAL(finished()), &eventLoop, SLOT(quit()));

    Because you cannot connect finished(QNetworkReply *) and quit() as they differ in a number of arguments.

    • rohieb
      November 8, 2010 at 16:46

      Apparently I can, because the code works fine for me 🙂 According to the Qt docs [1], you can pass fewer arguments from the signal to the slot if the slot has default arguments, but you cannot pass arguments the signal does not have.

      [1] http://doc.trolltech.com/4.6/signalsandslots.html#signals-and-slots-with-default-arguments

      • balta2ar
        November 8, 2010 at 19:47

        Wow, I didn’t know about that feature! Thanks for pointing out! 🙂

  2. Ben
    May 30, 2011 at 04:09

    hej rohieb, thanks for the tip. I had to do some batched net-requests (in their own thread) and the whole asynchronous-thing made that much more work than it should be.. I was about to create something like a state machine, just for some simple requests.. saved me at least some hours 😀

  3. kusuma
    May 31, 2011 at 12:39

    Hi,

    U r calling quit is fine.
    But where and how you are handling response.

    In my case i will get xml data to process.

    • rohieb
      May 31, 2011 at 14:09

      Note that the signal pnam->finished() is connected to eventLoop->quit(), so this is only for ending the event loop. The request itself is then passed to my user-defined function loginFinished() which was connected to the same pnam->finished() signal in line 3.

  4. enzo
    April 16, 2012 at 15:18

    could you not do something like:

    while QNetworkReply.isRunning():
    sleep(1)

    //process result here

    • rohieb
      April 16, 2012 at 15:43

      No, one second of sleep doesn’t guarantee that the reply hast already been processed. You have to call the event loop.

  5. Hodor88
    March 7, 2014 at 11:46

    Thanks alot. This helps me such much!°

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: