Qt World Summit: Register Today!

Usage for signals and slots in a class with multiple requests

  • 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?

  • Lifetime Qt Champion


    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.

  • 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?

  • Lifetime Qt Champion

    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.

  • 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...

  • Lifetime Qt Champion

    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.

  • Thank you @SGaist, you helped me a lot.

  • Lifetime Qt Champion

    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.

Log in to reply