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. Usage for signals and slots in a class with multiple requests
Forum Update on Monday, May 27th 2025

Usage for signals and slots in a class with multiple requests

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 2 Posters 1.3k 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.
  • D Offline
    D Offline
    Defohin
    wrote on last edited by Defohin
    #1

    I want to create a wrapper for an API in Qt using QNetworkAccessManager the thing is that this API requires me to login to get a token and then use the other methods with that token. I'm used to do that in other languages such as PHP that in a method I have login and that same method call the client, make the request and get the return but in Qt things are different, we have events, we have signals and slots, so I was wondering, what is the best way? Do I have to create a slot for each different request I make? Like one for login, another one for search, logout, etc? Or I'm missing something here?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      They do not necessarily need to be slots but that depends on how you are going to use wrapper. You will use signals and slots to handle the requests themselves though.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • D Offline
        D Offline
        Defohin
        wrote on last edited by Defohin
        #3

        I'm going to give you an example.
        I have to connect the finished signal of the QNetworkAccessManager in a slot, right? The thing is, I need a method called login so it can access the api, QNetworkAccessManager will give me the return of that request and in that response I will have the token.
        I also need to create a method called search, the API needs the token, the search also needs to get the response, so I have to connected to another slot in every method I need to call the API for a different request/response?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Not necessarily, you would rather connect the QNetworkReply finished signal. If you are using Qt 5, you can use lambdas to process your reply result as needed.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • D Offline
            D Offline
            Defohin
            wrote on last edited by
            #5

            You are right, I forgot the lambdas, I could connect the finished to a lambda, get the token and emit a custom signal that the token was received... or maybe I can just assign the token to a member, idk...

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Since you are going to re-use it and AFAIK only your wrapper will use it, there's no sense in emitting these data, store them locally.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • D Offline
                D Offline
                Defohin
                wrote on last edited by
                #7

                Thank you @SGaist, you helped me a lot.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  You're welcome !

                  If that answers your question, please mark the thread as solved using the "Topic Tools" button so other forum users may know a solution has been found :)

                  You have to first click on "Ask as question" and the open the menu again to mark the thread solved.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  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