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 relay the response of a asynchronous request
Forum Updated to NodeBB v4.3 + New Features

How to relay the response of a asynchronous request

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 466 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.
  • A Offline
    A Offline
    Abhi_Varma
    wrote on last edited by Abhi_Varma
    #1

    I'm new to Qt and I am not able to find a way to the following scenario where I am using asynchronous calls.

    void main()
    {
        ClassA objA;
        bool bResponse = objA.MakeRequest();
        .
        .
        .
        //Some other stuff 
    }
    
    //SIGNAL:
    bool ClassA::MakeRequest()
    {
        emit Request;
        //<-Server Response should come here
    }
    
    //Corresponding SLOT:
    void ClassB::OnRequest()
    {
        QUrl url(Some_Path);
        QNetworkRequest qRequest(url);
        qNetworkAccessManagerObj->post(qRequest, QByteArray());
    
    }
    
    //Following slot invoked when QNetworkAccessManger::finished(QNetworkReply*) signal is emitted
    
    void ClassB::OnServerResponseRecieved(QNetworkReply* qReply)
    {
        threadObj->serverResponse = qReply->readAll();
    }
    
    void ThreadClass::run()
    {
        bool bResponse;
        if (serverResponse == 500) //500 is Server Response Success Code
        {
            bResponse = true;
        }
        else
        {
            bResponse = false;
        }
    }
    

    I need to get back the response from EmitRequest(), Only on getting the response I need to continue executing other lines of code in the main().

    The question is that is it possible to relay the bResponse back to where I was emitting the request() SIGNAL so I can return the value? If not, Is there a workaround?

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

      Make ClassB emit a signal with the reply connected to ClassA


      I'm new to Qt

      ThreadClass

      This is a dangerous combo

      "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

      A Pl45m4P 2 Replies Last reply
      3
      • VRoninV VRonin

        Make ClassB emit a signal with the reply connected to ClassA


        I'm new to Qt

        ThreadClass

        This is a dangerous combo

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

        @VRonin But I want the server response to where Im emitting the signal. So that I can return it as part of the EmitRequest()

        jsulmJ 1 Reply Last reply
        0
        • A Abhi_Varma

          @VRonin But I want the server response to where Im emitting the signal. So that I can return it as part of the EmitRequest()

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

          @Abhi_Varma Why should a method called EmitRequest() return the response?! The method name says: it emits the request, it does not say it returns the response. If you want synchronous API (for what ever reason) then at least name the method differently. Why do you want blocking behaviour? It will block your UI.

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

          1 Reply Last reply
          1
          • VRoninV VRonin

            Make ClassB emit a signal with the reply connected to ClassA


            I'm new to Qt

            ThreadClass

            This is a dangerous combo

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by
            #5

            @VRonin said in How to relay the response of a asynchronous request:

            Make ClassB emit a signal with the reply connected to ClassA


            I'm new to Qt

            ThreadClass

            This is a dangerous combo

            LMAO :-)
            but true.


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            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