开发者

QNetworkAccessManager handling asynchronous thread

开发者 https://www.devze.com 2022-12-21 18:32 出处:网络
I am new to QT. I have created object class QNetworkAccessManager main window as parent. Also registered to SIGNAL finished. It is working fine.

I am new to QT. I have created object class QNetworkAccessManager main window as parent. Also registered to SIGNAL finished. It is working fine. But I want to know in which thread it will run. Will it block the main thread. If i need to perform sequence of get operation how should I need to write the code. Please give me some sample to understand concept pro开发者_如何学Cperly.


It certainly does not run in the main thread, the calls to get() are asynchronous.

For example this would keep firing get requests:

while (condition) {
    QNetworkRequest request;
    request.setUrl(QUrl(m_ServerURL);
    m_httpGetUpdatedFile->get(request);
}

You then have the slot for the finished signal which is processing the QNetworkReply. That slot basically should be getting called for each get request you make (even if it fails). If you need to keep track of when all your get requests have finished, you need to keep a track of how many you posted and then have your own finished flag or signal.


QNAM does use threads in the background, but this is completely invisible for your application code. Everything you see will run in the main thread.

QNAM works in the usual Qt way, it will emit signals when things happen, and you connect these signals to slots in your own code, which do things as much as they can. If they don't for example have enough data, then your slots must not block to wait for new data, instead they must return. And then they will be called again when/if there's more data (or you'll get another signal if for example connection was terminated).

Some links, in case you have not read these:

  • Qt signal & slot documentation
  • Qt networking documentation
0

精彩评论

暂无评论...
验证码 换一张
取 消