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: Associate response with request

QNetworkAccessManager: Associate response with request

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 2.3k Views 1 Watching
  • 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
    Hans Hupe
    wrote on last edited by
    #1

    Hello,

    I try to do a simple GET to a REST Service and want to know how I can identify/associate the request to the response I get in my SLOT function. For example I might send multiple requests the same manager and want to pass a messageID in order to read that ID in my SLOT function to associate the response with the responsible request.

    Any ideas how to do this?

    QNetworkAccessManager manager = new QNetworkAccessManager(this);
    connect(manager, SIGNAL(finished(QNetworkReply
    )),
    this, SLOT(replyFinished(QNetworkReply*)));

    manager->get(QNetworkRequest(QUrl("http://qt-project.org")));

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bsomervi
      wrote on last edited by
      #2

      You have two strategies:

      1. the QNetworkAccessManager::get() method returns a pointer to the QNetworkReply object instance that will handle the request so you could keep a container of the pointers and compare with the one returned in the finished signal,
      2. you can connect the finished signal of the returned QNetworkReply object instance to some slot on an object instance that is specific to that request.
      1 Reply Last reply
      1
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #3

        To add to what @bsomervi said. QNetworkReply is a QObject, so instead of keeping a list of them you can use its setProperty method to mark the object returned from get() with whatever meta-data you want and use that to identify the request.

        H 1 Reply Last reply
        1
        • Chris KawaC Chris Kawa

          To add to what @bsomervi said. QNetworkReply is a QObject, so instead of keeping a list of them you can use its setProperty method to mark the object returned from get() with whatever meta-data you want and use that to identify the request.

          H Offline
          H Offline
          Hans Hupe
          wrote on last edited by
          #4

          @Chris-Kawa Sounds good, but how can I ensure that the SLOT function is called after the setProperty method is executed?

          1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #5

            I'm not sure what you mean. You made a connect so the slot will get executed. What's the problem?

            H 1 Reply Last reply
            0
            • Chris KawaC Chris Kawa

              I'm not sure what you mean. You made a connect so the slot will get executed. What's the problem?

              H Offline
              H Offline
              Hans Hupe
              wrote on last edited by
              #6

              @Chris-Kawa so after the get, I set a property of the returned QNetworkReply object. Then in the the SLOT function I can access the property, is that the idea?

              1 Reply Last reply
              0
              • Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Yes, that's it.

                1 Reply Last reply
                0

                • Login

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