pages tagged multithreadingrohieb.namehttps://rohieb.name/blag/tag/multithreading/rohieb.nameikiwiki2013-09-19T05:04:01ZQt: (Nearly) synchronous QNetworkAccessManager callshttps://rohieb.name/blag/post/qt-nearly-synchronous-qnetworkaccessmanager-calls/rohieb
CC-BY-SA 3.0
2013-09-19T05:04:01Z2010-07-07T22:00:00Z
<p>The <a href="http://doc.qt.nokia.com/4.6/qnetworkaccessmanager.html">QNetworkAccessManager</a> 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:</p>
<div class="highlight-c"><pre class="hl">QNetworkAccessManager <span class="hl opt">*</span> pnam <span class="hl opt">=</span> <span class="hl kwa">new</span> <span class="hl kwd">QNetworkAccessManager</span><span class="hl opt">(</span><span class="hl kwa">this</span><span class="hl opt">);</span>
<span class="hl slc">// the slot was declared at another place</span>
<span class="hl kwd">connect</span><span class="hl opt">(</span>pnam<span class="hl opt">,</span> <span class="hl kwd">SIGNAL</span><span class="hl opt">(</span><span class="hl kwd">finished</span><span class="hl opt">(</span>QNetworkReply <span class="hl opt">*)),</span> <span class="hl kwa">this</span><span class="hl opt">,</span>
<span class="hl kwd">SLOT</span><span class="hl opt">(</span><span class="hl kwd">loginFinished</span><span class="hl opt">(</span>QNetworkReply<span class="hl opt">*)));</span>
QNetworkRequest <span class="hl kwd">req</span><span class="hl opt">(</span><span class="hl kwd">QUrl</span><span class="hl opt">(</span><span class="hl str">"http://foo.bar"</span><span class="hl opt">));</span>
pnam<span class="hl opt">-></span><span class="hl kwd">post</span><span class="hl opt">(</span>req<span class="hl opt">,</span> postData<span class="hl opt">);</span>
<span class="hl slc">// execute an event loop to process the request (nearly-synchronous)</span>
QEventLoop eventLoop<span class="hl opt">;</span>
<span class="hl slc">// also dispose the event loop after the reply has arrived</span>
<span class="hl kwd">connect</span><span class="hl opt">(</span>pnam<span class="hl opt">,</span> <span class="hl kwd">SIGNAL</span><span class="hl opt">(</span><span class="hl kwd">finished</span><span class="hl opt">(</span>QNetworkReply <span class="hl opt">*)), &</span>eventLoop<span class="hl opt">,</span> <span class="hl kwd">SLOT</span><span class="hl opt">(</span><span class="hl kwd">quit</span><span class="hl opt">()));</span>
eventLoop<span class="hl opt">.</span><span class="hl kwd">exec</span><span class="hl opt">();</span>
</pre></div>
<p>This way my user-defined slot for the <code>pnam->finished()</code> signal was
called immediately, and I could be sure to have the HTTP reply at the
end of this code snippet.</p>
<p>Found here: <a href="http://lists.qt.nokia.com/public/qt-interest/2010-April/022031.html">Qt-Interest Mailing List: QNetworkAccessManager and
QNetworkReply, synchronous</a></p>