QNetworkAccessManager object not receiving the response when closing application
-
Hi Qt Community, I have a desktop application written in qt and a server application thats written in .Net core. I'm trying to make an api call when I'm closing my application. Server is getting my call, processing it and sending back the response but my QNetworkAccessManager object is not emitting the finished signal. Other api call that are made when using the application are working fine, Only when user click the X button to close the app at that time whatever api calls i make are not getting the response. I have checked all the ports and signal slots are in connected state and server is sending me the response. Below is the portion of the code that shows the flow.
App.h
enum class RESPONSE_STATE { RESPONSE_RECEIVED_STATE = 0, RESPONSE_REQUESTED_STATE, RESPONSE_INVALID } class CApp : public QtSingleApplication { Q_OBJECT private: //member variables RESPONSE_STATE m_eApiCallResponseState; QNetworkAccessManager* m_pNetworkManager; public: CApp( ); ~CApp(); public slots: void OnClosing(); void OnResponseReceived(QNetworkReply* pReply); };App.cpp
CApp::CApp () : m_eApiCallResponseState(RESPONSE_STATE ::RESPONSE_INVALID) , m_pNetworkManager(new QNetworkAccessManager(this) { m_pNetworkManager->setProxy(QNetworkProxy::NoProxy); connect(m_pNetworkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(OnResponseReceived(QNetworkReply*))); connect(this, SIGNAL(lastWindowClosed()), this, SLOT(onClosing())); } CApp::~CApp() { // Disconnect all the signal slot connection // Close all the ports // Delete member variables } void CApp::onClosing() { m_eApiCallResponseState = RESPONSE_REQUESTED_STATE; //I'm making api call here using m_pNetworkManager do { // Checked port is still connected } while(m_eApiCallResponseState != RESPONSE_REQUESTED_STATE); } void CApp::OnResponseReceived(QNetworkReply* pReply) { //Handle the response m_eApiCallResponseState = RESPONSE_RECEIVED_STATE ; }I have made the same api call from other place im getting the response. On making the call from the onClosing().
m_pNetworkManageris not emitting thefinished()signal. introduced do while loop just to make sure response is coming or not. Any help is much appreciated. -
Hi Qt Community, I have a desktop application written in qt and a server application thats written in .Net core. I'm trying to make an api call when I'm closing my application. Server is getting my call, processing it and sending back the response but my QNetworkAccessManager object is not emitting the finished signal. Other api call that are made when using the application are working fine, Only when user click the X button to close the app at that time whatever api calls i make are not getting the response. I have checked all the ports and signal slots are in connected state and server is sending me the response. Below is the portion of the code that shows the flow.
App.h
enum class RESPONSE_STATE { RESPONSE_RECEIVED_STATE = 0, RESPONSE_REQUESTED_STATE, RESPONSE_INVALID } class CApp : public QtSingleApplication { Q_OBJECT private: //member variables RESPONSE_STATE m_eApiCallResponseState; QNetworkAccessManager* m_pNetworkManager; public: CApp( ); ~CApp(); public slots: void OnClosing(); void OnResponseReceived(QNetworkReply* pReply); };App.cpp
CApp::CApp () : m_eApiCallResponseState(RESPONSE_STATE ::RESPONSE_INVALID) , m_pNetworkManager(new QNetworkAccessManager(this) { m_pNetworkManager->setProxy(QNetworkProxy::NoProxy); connect(m_pNetworkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(OnResponseReceived(QNetworkReply*))); connect(this, SIGNAL(lastWindowClosed()), this, SLOT(onClosing())); } CApp::~CApp() { // Disconnect all the signal slot connection // Close all the ports // Delete member variables } void CApp::onClosing() { m_eApiCallResponseState = RESPONSE_REQUESTED_STATE; //I'm making api call here using m_pNetworkManager do { // Checked port is still connected } while(m_eApiCallResponseState != RESPONSE_REQUESTED_STATE); } void CApp::OnResponseReceived(QNetworkReply* pReply) { //Handle the response m_eApiCallResponseState = RESPONSE_RECEIVED_STATE ; }I have made the same api call from other place im getting the response. On making the call from the onClosing().
m_pNetworkManageris not emitting thefinished()signal. introduced do while loop just to make sure response is coming or not. Any help is much appreciated.@AbhiVarma You should override closeEvent like shown in https://stackoverflow.com/questions/17480984/qt-how-do-i-handle-the-event-of-the-user-pressing-the-x-close-button and delay the closing. Else your app is closed before it gets the response.
-
@AbhiVarma You should override closeEvent like shown in https://stackoverflow.com/questions/17480984/qt-how-do-i-handle-the-event-of-the-user-pressing-the-x-close-button and delay the closing. Else your app is closed before it gets the response.
-
@jsulm I have tried to call the api call from MainWindow closeEvent before accepting the event but still im not getting the response
Do you have any suggested method for debugging the network manager.
@AbhiVarma said in QNetworkAccessManager object not receiving the response when closing application:
before accepting the event
That's the point: you should NOT accept the event, because else your app is closed. Ignore the event. As sson as you get the response close the application (you will get closeEvent again, so you will need to remember that you already got the response and accept the event).
-
@AbhiVarma said in QNetworkAccessManager object not receiving the response when closing application:
before accepting the event
That's the point: you should NOT accept the event, because else your app is closed. Ignore the event. As sson as you get the response close the application (you will get closeEvent again, so you will need to remember that you already got the response and accept the event).
@jsulm before accepting the event i was waiting for 30 seconds to 1 min but still i didnt get finished signal even though server sent me response.
-
@jsulm before accepting the event i was waiting for 30 seconds to 1 min but still i didnt get finished signal even though server sent me response.
@Abhi_Varma said in QNetworkAccessManager object not receiving the response when closing application:
i was waiting for 30 seconds to 1 min
Blocking the event loop? This is not something you should do in an event driven applications. As long as event loop is blocked your slots will not be called.
-
@Abhi_Varma said in QNetworkAccessManager object not receiving the response when closing application:
i was waiting for 30 seconds to 1 min
Blocking the event loop? This is not something you should do in an event driven applications. As long as event loop is blocked your slots will not be called.
-
@jsulm Thanks for the solution. It working now. But i would like to understand why we have to make calls and get all the responses before window closes
@AbhiVarma Well, how should your app receive the response if it is closed? It has to wait for the response...
-
@AbhiVarma Well, how should your app receive the response if it is closed? It has to wait for the response...
-
@jsulm what if i do the following.
do { qAppObject->processEvents(QEventLoop::WaitForMoreEvents, 500); } while(m_eApiCallResponseState != RESPONSE_REQUESTED_STATE);@AbhiVarma Why?
Why don't you do it in a clean way as I suggested?
But it is up to you to write dirty code which may fail... -
@AbhiVarma Why?
Why don't you do it in a clean way as I suggested?
But it is up to you to write dirty code which may fail...@jsulm I am handling it in the closeEvent. I'm waiting in the closeEvent until i get my response. using the following loopk. only once i get response and exiting the loop and accepting the event
do
{
qAppObject->processEvents(QEventLoop::WaitForMoreEvents, 500);
}
while(m_eApiCallResponseState != RESPONSE_REQUESTED_STATE); -
@jsulm I am handling it in the closeEvent. I'm waiting in the closeEvent until i get my response. using the following loopk. only once i get response and exiting the loop and accepting the event
do
{
qAppObject->processEvents(QEventLoop::WaitForMoreEvents, 500);
}
while(m_eApiCallResponseState != RESPONSE_REQUESTED_STATE);@AbhiVarma I already explained you how you should do it without any dirty workarounds like local event loop...