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. QNetworkAccessManager better way of using it
Forum Update on Monday, May 27th 2025

QNetworkAccessManager better way of using it

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 710 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.
  • Y Offline
    Y Offline
    yugosonegi
    wrote on last edited by
    #1

    I'm new to Qt and it's design as I come from other languages and I have a question regarding QNetworkAccessManager and the way it works with signal and slots. I noticed that if I need to do a request using QNetworkAccessManager I need to connect to a slot in order to get the return using QNetworkReply but as I come from other languages that a simple request I could just set to a variable instead of needing to connect the return to another method, I was wondering:

    If I need to create an API wrapper like for example: I need to create a request to authenticate, another request to query, another request to download, how would that look like? Is there an example of making more than one request in the same class to an API?

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

      Hi
      The reason for the slots is that QNetworkAccessManager is asyncon.
      So it will not wait/stay at the line until something received but "call you back" instead

      However since you can now use lambdas you can keep it very local (demo code, not complete)

      QNetworkRequest request = QNetworkRequest(QUrl(xxxxx));
      
      QNetworkReply* reply = m_manager->get(request);
      
      connect(reply, &QNetworkReply::finished, [this, reply]() // <-- lamda as slot
      {
          qDebug() << sender();
          qDebug() << reply->errorString();
          qDebug() << reply->readAll(); 
          reply->deleteLater();
      });
      
      
      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