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. How to handle many QNetworkReplies ?
QtWS25 Last Chance

How to handle many QNetworkReplies ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
networking
8 Posts 3 Posters 1.9k 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.
  • O Offline
    O Offline
    OpenGL
    wrote on 11 Apr 2018, 14:22 last edited by
    #1

    Hi.
    I'm looking for the best (or least bad) way to handle all the QNetworkReply objects that are created.

    As I understand it I should do like this

    this->myQNetworkReply = this->myQNetworkAccessManager->get( myQNetworkRequest);
    
    connect (this->myQNetworkReply, &QNetworkReply::finished, this, SLOT(some_slot_to_read_the result_from_myQNetworkReply));
    

    Now, If I want to send 10 queries repeatedly how do I best do this?
    Should I do something like this (I use a stupid while loop instead of using a QTimer to make the example code shorter)

    QNetworkReply* tmp;
    QList<QNetworkReply*> myList;
    while (1 == 1) {
         tmp = this->myQNetworkAccessManager->get(myQNetworkRequest1);
         myList.append(tmp);
         connect(tmp, ... like above);
    
         tmp = this->myQNetworkAccessManager->get(myQNetworkRequest2);
         myList.append(tmp);
         connect(tmp, ... like above);
    
         ....
     
         tmp = this->myQNetworkAccessManager->get(myQNetworkRequest10);
         myList.append(tmp);
         connect(tmp, ... like above);
    }
    

    This seems like a lot of code, and a lot of connections are created . Should I do the connects, or should I instead iterate the QList and look for replies that isFinished() and then processes the data?
    What is the best way?

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 11 Apr 2018, 14:27 last edited by
      #2

      Does some_slot_to_read_the result_from_myQNetworkReply change from one request to the other?

      "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

      O 1 Reply Last reply 11 Apr 2018, 14:40
      0
      • V VRonin
        11 Apr 2018, 14:27

        Does some_slot_to_read_the result_from_myQNetworkReply change from one request to the other?

        O Offline
        O Offline
        OpenGL
        wrote on 11 Apr 2018, 14:40 last edited by
        #3

        @VRonin
        Good point.
        The 10 requests are different so the slot functions must handle different data, so in theory they must be different.
        So in theory, 10 different slot functions are needed.

        P 1 Reply Last reply 11 Apr 2018, 15:12
        0
        • V Offline
          V Offline
          VRonin
          wrote on 11 Apr 2018, 14:49 last edited by
          #4

          The room for improvement from your code is very limited then

          "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

          1 Reply Last reply
          1
          • O OpenGL
            11 Apr 2018, 14:40

            @VRonin
            Good point.
            The 10 requests are different so the slot functions must handle different data, so in theory they must be different.
            So in theory, 10 different slot functions are needed.

            P Offline
            P Offline
            Pablo J. Rogina
            wrote on 11 Apr 2018, 15:12 last edited by
            #5

            @OpenGL said in How to handle many QNetworkReplies ?:

            The 10 requests are different so the slot functions must handle different data

            So why don't you treat your only slot as a "factory" where you then call the other 10 methods based on some condition from the data received in the response?

            In addition, I like to connect the QNetworkAccessManager::finished() signal instead of individual QNetworkReply::finished() signal

            connect(&networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(handleCommandResponse(QNetworkReply*)));
            

            Upvote the answer(s) that helped you solve the issue
            Use "Topic Tools" button to mark your post as Solved
            Add screenshots via postimage.org
            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

            V 1 Reply Last reply 11 Apr 2018, 16:56
            1
            • P Pablo J. Rogina
              11 Apr 2018, 15:12

              @OpenGL said in How to handle many QNetworkReplies ?:

              The 10 requests are different so the slot functions must handle different data

              So why don't you treat your only slot as a "factory" where you then call the other 10 methods based on some condition from the data received in the response?

              In addition, I like to connect the QNetworkAccessManager::finished() signal instead of individual QNetworkReply::finished() signal

              connect(&networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(handleCommandResponse(QNetworkReply*)));
              
              V Offline
              V Offline
              VRonin
              wrote on 11 Apr 2018, 16:56 last edited by
              #6

              @Pablo-J.-Rogina said in How to handle many QNetworkReplies ?:

              I like to connect the QNetworkAccessManager::finished() signal instead of individual QNetworkReply::finished() signal

              I desagree. There should be only 1 QNAM per app and your slot would get call even when totally unrelated requests are triggered.
              This is the equivalent of processing an event of a widget from QApplication::event instead of QWidget::event

              "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

              1 Reply Last reply
              1
              • P Offline
                P Offline
                Pablo J. Rogina
                wrote on 11 Apr 2018, 17:13 last edited by
                #7

                @VRonin maybe not being an English-native speaker made my comment not clear enough. Yes, I'm just using only one QNAM per application.

                Upvote the answer(s) that helped you solve the issue
                Use "Topic Tools" button to mark your post as Solved
                Add screenshots via postimage.org
                Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                V 1 Reply Last reply 11 Apr 2018, 17:37
                0
                • P Pablo J. Rogina
                  11 Apr 2018, 17:13

                  @VRonin maybe not being an English-native speaker made my comment not clear enough. Yes, I'm just using only one QNAM per application.

                  V Offline
                  V Offline
                  VRonin
                  wrote on 11 Apr 2018, 17:37 last edited by
                  #8

                  @Pablo-J.-Rogina said in How to handle many QNetworkReplies ?:

                  I'm just using only one QNAM per application.

                  That means that, if you connect to QNetworkAccessManager::finished() instead of QNetworkReply::finished() yor slot can be triggered even when a request from a totally unrelated class is finished

                  "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

                  1 Reply Last reply
                  1

                  1/8

                  11 Apr 2018, 14:22

                  • Login

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