Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Can we make post request using QNetworkAccessManager in secondary thread?

Can we make post request using QNetworkAccessManager in secondary thread?

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 499 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hetal
    wrote on last edited by
    #1

    I have multiple questions.

    • Can we make network request from secondary thread using QNetworkAccessManager ? Here we are making sure that the QNetworkAcessManager is created in secondary thread only.

    • I want to make multiple network request in my application. Among those request there are many blocking request which blocks the UI. So how can we achieve that?

    • Also in the documentation it is written that QNetworkAccessManager queues the QnetworkRequest by itself then how can we get the queued request count?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      hi
      1)
      Yes, you can use the worker approach
      https://wiki.qt.io/QThreads_general_usage
      and run the QNetworkAccessManager there as far as i know it should work fine.
      2)
      You can run 6 concurrent requests and handle that in an asynchronous manner. Using lambdas you can make the code very compact
      and local but its not blocking as that is not supported.

      There is a way to do it

      QNetworkAccessManager qnam;
      QNetworkReply *reply = qnam.get(QNetworkRequest(QUrl(…)));
      QEventLoop loop;
      QObject::connect(reply, SIGNAL (finished()), &loop, SLOT (quit()));
      loop.exec();
      

      But using QEventLoop all over the application is the road to odd side effects and not really recommended.

      So why must it be blocking ?
      Cant you just use the normal api and simply handle the requests when they come in ?
      Do you need more than 6 requests at the same time ?

      I have not seen any access to that info but check out the code and see if you are lucky :)
      https://code.woboq.org/qt5/qtbase/src/network/access/qnetworkaccessmanager.cpp.html

      1 Reply Last reply
      3

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved