-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
[REQ][Qt5] Make response objects queueable #3063
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Please do this this
OAIPetApi* api
api->findPetsByStatus(status); // Non blocking call
connect(api, &OAIPetApi::findPetsByStatusSignal, this, validator, Qt::QueuedConnection);
|
This doesn't work. The output error is the same
|
Yes this is needed qRegisterMetaType<OAIPet>( "OAIPet" ); Registering this is easy. |
Sorry, but even after registering the class it still doesn't work, I must be doing something wrong if it's working for you. By the way, I'm using openapi-generator-cli 4.0.0 to generate the code if it makes any difference. This is the code I'm trying, using this docker https://hub.docker.com/r/openapitools/openapi-petstore as a server. I skipped the code related to QThread since it's enought to make it work with Qt::QueuedConnection
|
I will try it out and let you know |
Seems like it does not work for me. The callback arrives in the thread where the API was created and where the event loop is running. |
I just noticed we were missing something else:
To make the request also run in the thread, findPetsByStatus should be a slot and not a regular method. This way we can connect a signal to this method as shown in the following snippet.
Either way, the callback method cannot queue OAIPet back to the main thread. The same would happen the other way around if we wanted to make addPet to run in the thread. |
Yes this is needed.
Indeed, how the objects are moved around however is outside this api client. so I am not sure how to proceed. |
Ok, I'll try to explain: To be able to make requests in a thread and get the response back to the main thread, we need to use the signals&slots mechanism with queued connections. So to sum up, I think there are 3 things to be done:
I'm not sure if it's clear what I'm trying to achieve. Please ask if needed |
It is clear what you are trying to do, I am just wondering how, without breaking the current compatibility for current users. |
Ok great, I wasn't sure it was clear. Also I've made some progress. I only needed to add Q_DECLARE_METATYPE(OpenAPI::OAIPet) to OAIPet header, and force the connection to Qt::QueuedConnection as you mentioned earlier. So maybe the only change needed is to register all generated clases as metatypes by adding Q_DECLARE_METATYPE at the bottom of the header, and this doesn't seem to have any side effects that could affect backwards compatibility. Hope it helps |
Just noticed that error signals contain a parameter Changing to either |
I think the parameters in the signals should not be reference anyway. because they are created on stack |
I created the PR. I think the default copy constructor is good enough. |
Great, Thank you! |
Is your feature request related to a problem?
No, it's an improvement
Please describe.
I think it would be great if we could run requests in a QThread, but right now it's not possible because the returned objects cannot be queueable (signals&slots) from one thread to a different thread.
This is in order to be able to run requests in background without freezing the UI.
Describe the solution you'd like
I think if the response objects where pointers to qobjects instead of regular objects it should work. Notice that they should be registered with qRegisterMetaType() ( https://doc.qt.io/qt-5/qt.html#ConnectionType-enum )
Describe alternatives you've considered
Right now the alternative is to manually create a proxy class that makes use of the generated client to make the request, receive the response and then convert the response objects to some pointers to custom qobjects, which are queueable. So the purpose of this suggestion is to avoid this workaround
Additional context
The text was updated successfully, but these errors were encountered: