blag: convert (and slightly improve) blogposts from rohieb.wordpress.org
[www-rohieb-name.git] / blag / post / qt-nearly-synchronous-qnetworkaccessmanager-calls.mdwn
diff --git a/blag/post/qt-nearly-synchronous-qnetworkaccessmanager-calls.mdwn b/blag/post/qt-nearly-synchronous-qnetworkaccessmanager-calls.mdwn
new file mode 100644 (file)
index 0000000..373d583
--- /dev/null
@@ -0,0 +1,37 @@
+[[!meta title="Qt: (Nearly) synchronous QNetworkAccessManager calls"]]
+[[!meta date="2010-07-08"]]
+[[!meta author="rohieb"]]
+[[!meta license="CC-BY-SA 3.0"]]
+
+The [QNetworkAccessManager][0] 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:
+
+[[!format c """
+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][1]
+
+[0]: http://doc.qt.nokia.com/4.6/qnetworkaccessmanager.html
+[1]: http://lists.qt.nokia.com/public/qt-interest/2010-April/022031.html
+
+[[!tag howto programming multithreading network Qt signals slots]]
This page took 0.024002 seconds and 4 git commands to generate.