Solved Multiple requests in the same method
-
Amazing amazing, thank you for making me see this in a whole new way. I will be playing with this in order to learn more.
-
@Chris-Kawa I was playing around and I have two questions:
1 - Is it better for me to connect with the
QNetworkAccessManager
finished in order to send theQNetworkReply
to another method such asprocessJson
or I can have oneQNetworkReply
pointer member and reuse that for other requests?2 - What about error handling for each step?
-
The answer to both questions is "whatever you want or will integrate nicely with the rest of your code".
To give it a little more substance:1 - It's generally suggested to use single QNAM instance. Since you mentioned you can have multiple requests in flight at the same time connecting to the QNAM's
finished
signal might be inconvenient. You would have to somehow distinguish which series of requests it is and which "stage" of it it is. That's of course doable. You could for example do something like this:auto response = qnam.get(request); response->setProperty("request_type", foo); //foo could be an enum indicating login, logout, search etc. response->setProperty("request_stage", bar); // bar could be an enum indicating json retrieval or the following request
and then in the slot connected to QNAM's
finished(QNetworkReply*)
signal you could check these properties and do a big "switch". This would work but can get kinda messy as those switches tend to grow over time and gather unrelated functionalities.Another approach is to connect not to the QNAM's
finished
signal but to the response'sfinished
signal. This way you can "string" the requests together and you don't have to do a big switch to see which one it is. every method knows what it receives and can start the next stage.
To get the response object you can use a little lambda on connection site:void Searcher::search(const QString& what) { auto reply = qnam.get( /* build the request from what*/ ); connect(reply, &QNetworkReply::finished, [=]{ processJson(reply); }); } void Searcher::processJson(QNetworkReply* reply) { ... }
Or, if you want to spare yourself the lambda, you can also do it like this:
void Searcher::search(const QString& what) { auto reply = qnam.get( /* build the request from what*/ ); connect(reply, &QNetworkReply::finished, this, &Searcher::processJson); } void Searcher::processJson() { auto reply = qobject_cast<QNetworkReply*>(sender()); ... }
Some people will tell you that
sender()
is bad and you shouldn't use it (even the docs says that), but if you keep methods that use it private I consider that internal piping and a valid use forsender()
. It is considered bad because you never know how the slot is called if it's public, but if it's private you, as the designer of the class should know and be responsible for calling it right.
Anyway, lambda or not, this is one way to do it.2 - That one is pretty simple really. Apart from connecting to
finished
signal of the reply connect also to theerror
andsslErrors
(if you use https). If you don't care what particular error happened just emitsearchFinished
with an empty list of results. If you do care (e.g. want to show some message to the user) instead of emittingsearchFinished
you could emit something likesearchFailed(QString& reason)
or you could add another param to thesearchFinished
signal e.g. an enum that would indicate what type of error occurred or a message string where empty string would indicate a successful search and non-empty would contain the error message. -
@Chris-Kawa So in this case I can use the same
QNetworkReply
with another request right? But where do I delete the reply? I see in the docs that they usually calldeleteLater
in the finish slot, if I want to reuse the reply where to delete it? -
You can't reuse a reply. A new reply object is created by QNAM and returned to you by each call to
get()
,post()
etc.
You can store and reuse a pointer to it but IMO this can lead to bugs if you mix up your requests. I wouldn't recommend that. It's better to just connect to what you need and be done with it.You can call
deleteLater
in the slot connected tofinished()
. As said before - you can't reuse the reply object, just the pointer to it and this is the trouble I mentioned - it's easy to mess up and delete the wrong thing or at the wrong time. I'd suggest not to store it at all and rely on either function param orsender()
in the slot connected tofinished()
signal. -
@Chris-Kawa Thank you very much
-
@Chris-Kawa Playing around I noticed that I needed extra information on my
search
method, two more arguments asQString
. The thing is that I will be needing that two extra arguments later in another method/slot.More or less like this:
search
>processJson
>processProducts
I will need the extra arguments in the
processProducts
.The option I thought was:
1 - store the values passed to
search
in a member and get later in another method.Is there a better alternative for that?
-
If one instance of your class does only one search at a time then sounds like a reasonable thing to store the extra state as members. No point in complicating things.
-
Yes, it will be one search per instance, because I will create a
QTabWidget
and eachQWidget
of the tab will have an instance of theSearcher
. I mean, the new tab will be created after the user fills a line edit and click on the search button. -
@Chris-Kawa I have a few more questions:
The searcher will also be a downloader.
The idea behind this application is that the user can search for a product and the product will appear in a tree view. I will explain the tree view.
I want to do something like this:
1 - The user fills a line edit with the product he wants to search and press the search button.
2 - A new tab will be created in theQTabWidget
with a tree view and an instance of theSearcher
.
3 - When the search is done andSearcher
emits a signal that the search is done I want to display the results in a tree view.
4 - The results thatSearcher
will return is a collection ofProduct
s, a struct with a few information such as the name, color and other information, but the most important one, it will have a link to a zip file.
5 - I want to create a button on the tree view that is calledDownload
in eachProduct
which contains the zip link.
6 - When the user clicks on that button I want to download that zip file and get the file names and put as children to the download clicked item on the tree view.For me its kinda complicated, any ideas of what I can do to archive that?
The
Product B
has a zip link so it will have a download button on the tree view. Imagine that the user clicked on the button, it downloaded the zip file, got its the file names and put as children forProductB
.