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. Managing responses from API

Managing responses from API

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 470 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.
  • A Offline
    A Offline
    Abdurahman_Gulamkadirov
    wrote on last edited by
    #1

    Hi everyone. I'm working on a project. Almost every window of my app should represent different data in QTableView, for doing that they gets data from different URLs. Requesting data should be asynchronously. I'm going to write clean code as possible. What do you advice? I'm so confused.

    jsulmJ 1 Reply Last reply
    0
    • A Abdurahman_Gulamkadirov

      Hi everyone. I'm working on a project. Almost every window of my app should represent different data in QTableView, for doing that they gets data from different URLs. Requesting data should be asynchronously. I'm going to write clean code as possible. What do you advice? I'm so confused.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Abdurahman_Gulamkadirov said in Managing responses from API:

      for doing that they gets data from different URLs

      So, it is a REST API?
      You can access such APIs using https://doc.qt.io/qt-5/qnetworkaccessmanager.html which is already asynchronous.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply
      2
      • jsulmJ jsulm

        @Abdurahman_Gulamkadirov said in Managing responses from API:

        for doing that they gets data from different URLs

        So, it is a REST API?
        You can access such APIs using https://doc.qt.io/qt-5/qnetworkaccessmanager.html which is already asynchronous.

        A Offline
        A Offline
        Abdurahman_Gulamkadirov
        wrote on last edited by Abdurahman_Gulamkadirov
        #3

        @jsulm Yes, it is. I created a class called NetworkManager that inherited by QObject. The class has QNetworkAccessManager. And there are methods called get and post, also there is a slot that takes QNetworkReply*. The class gives data in QJsonDocument that comes with response, using is simple:

        NetworkManager* manager(this);
        ...
        manager->post(url, data);
        

        So I wanted to use manager in all windows, but connecting signals/slots to every windows is not clever solution. I just didn't want to create manager object for every windows

        jsulmJ 1 Reply Last reply
        0
        • A Abdurahman_Gulamkadirov

          @jsulm Yes, it is. I created a class called NetworkManager that inherited by QObject. The class has QNetworkAccessManager. And there are methods called get and post, also there is a slot that takes QNetworkReply*. The class gives data in QJsonDocument that comes with response, using is simple:

          NetworkManager* manager(this);
          ...
          manager->post(url, data);
          

          So I wanted to use manager in all windows, but connecting signals/slots to every windows is not clever solution. I just didn't want to create manager object for every windows

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Abdurahman_Gulamkadirov said in Managing responses from API:

          but connecting signals/slots to every windows is not clever solution

          Well, you anyway need to get the response for each request somehow, right? manager->post(url, data) could return an object which could be used to connect a signal to get response as soon as it arrives:

          Response* response = manager->post(url, data);
          connect(response, &Response::finished, this, &MyClass::handleResponse);
          connect(response, &Response::finished, response, &QObject::deleteLater);
          

          You can share one NetworkManager instance between your windows as each get/post call returns a QNetworkReply which is used to get the response, so you know which response belongs to which request.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          A 1 Reply Last reply
          2
          • jsulmJ jsulm

            @Abdurahman_Gulamkadirov said in Managing responses from API:

            but connecting signals/slots to every windows is not clever solution

            Well, you anyway need to get the response for each request somehow, right? manager->post(url, data) could return an object which could be used to connect a signal to get response as soon as it arrives:

            Response* response = manager->post(url, data);
            connect(response, &Response::finished, this, &MyClass::handleResponse);
            connect(response, &Response::finished, response, &QObject::deleteLater);
            

            You can share one NetworkManager instance between your windows as each get/post call returns a QNetworkReply which is used to get the response, so you know which response belongs to which request.

            A Offline
            A Offline
            Abdurahman_Gulamkadirov
            wrote on last edited by Abdurahman_Gulamkadirov
            #5

            @jsulm Thanks, it helped! I didn't think about handle replie's signals

            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