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. Pass pointer over QNetworkReply slot
Forum Updated to NodeBB v4.3 + New Features

Pass pointer over QNetworkReply slot

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 569 Views 2 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.
  • M Offline
    M Offline
    Mr Gisa
    wrote on last edited by
    #1

    I have a class called Resolver method that is called handleResult(Result *result).
    The class Result has a member that is a QUrl and others.
    This handleResult method creates a request using the url from the result pointer and connects the QNetworkReply signal to handlePage.
    The thing here is that I need that result pointer in the handlePage slot as well so I can modify the members in there.
    Beforehand, I can't put the Result as a member of Resolver cause the handleResult is being called several times, meaning that it won't probably be the right Result when handlePage is called (as QNetworkAccessManager is async).
    So how can I pass that pointer to handlePage?

    raven-worxR 1 Reply Last reply
    0
    • M Mr Gisa

      I have a class called Resolver method that is called handleResult(Result *result).
      The class Result has a member that is a QUrl and others.
      This handleResult method creates a request using the url from the result pointer and connects the QNetworkReply signal to handlePage.
      The thing here is that I need that result pointer in the handlePage slot as well so I can modify the members in there.
      Beforehand, I can't put the Result as a member of Resolver cause the handleResult is being called several times, meaning that it won't probably be the right Result when handlePage is called (as QNetworkAccessManager is async).
      So how can I pass that pointer to handlePage?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Mr-Gisa
      can you show some code? Especially where you create the network reply and connect it to handlePage().
      I guess the easiest would probably be to connect the network reply to a lambda slot and capture all the pointers/data you need.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #3

        std::function. for example:

        void handlePage(QNetworkReply* sender, Result *result);

        connect(reply,&QNetworkReply::finished,this,std::bind(&MyClass::handlePage,this,reply,result));

        you need to make sure result doesn't get deleted in the meantime though

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        raven-worxR 1 Reply Last reply
        4
        • VRoninV VRonin

          std::function. for example:

          void handlePage(QNetworkReply* sender, Result *result);

          connect(reply,&QNetworkReply::finished,this,std::bind(&MyClass::handlePage,this,reply,result));

          you need to make sure result doesn't get deleted in the meantime though

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #4

          to add up to @VRonin the lambda approach:

          QNetworkReply* reply = ...
          Result* result = ...
          connect( reply, &QNetworkReply::finished, this, [this, reply, result]() {
               // do whatever you have to do directly in here or call "this->handleResult(reply, result);"
               delete reply;
               delete result;
          });
          

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          5

          • Login

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