Does QNetworkAccessManager::post() work in QMainWindow::closeEvent()?
-
I have a function that uses QNetworkAccessManager::post() to run a PHP script that writes some data on my server. The program runs on macOS 10.15.
This function runs fine when invoked at the beginning of the program, and the data does get written on the server.
However, when the function is called during QMainWindow::closeEvent(), the script is never run. The post() call returns QNetworkReply::NoError.
Why is this happening? Is the application shut down at this stage, so it can't send HTTP requests? Is there an earlier point that I can intercept, when the program is shutting down?
Here is a code snippet:
QNetworkReply *reply; QString urlStr("https://foo.com/bar.php?some&data"); QUrl url(urlStr); if(!url.isValid()) { QString urlError = url.errorString(); qDebug() << "\n" << urlError << "\n"; } QNetworkRequest *request = new QNetworkRequest(url); request->setHeader(QNetworkRequest::ContentTypeHeader, "text/plain"); QByteArray content = urlStr; QNetworkAccessManager *nam = new QNetworkAccessManager; if(request != NULL && nam != NULL ) { reply = nam->post(*request, content); QNetworkReply::NetworkError netret = reply->error(); // QNetworkReply::NoError }
-
You can re-implement the closeEvent, wait until your post task is finished and then continue to close your app by accepting the event, if this was your question ;)
The code you are showing above, is this in your closeEvent or just a random function? You can't expect to get a proper result, when closing your app while your NetMan is still doing some stuff
-
I think that is what we're doing. The function above is named 'xmitStats' and is called from our closeEvent() handler:
void MainWindow::closeEvent(QCloseEvent *event) { // end of session - transmit stats xmitStats(); event->accept(); QMainWindow::closeEvent(event); }
Is this OK? I wonder why is it not working?
-
This doesn't ensure that NetMan is finished before you accept the event. Use
QNetworkAccessManager::finished
signal and then continue with your closeEvent.
You could use a lambda for that purpose.
Something likeconnect(netMan, &QNetworkAccessManager::finished, [=](QNetworkReply *) { // Check for reply error or do some other stuff here }
-
I'm sorry for being slow, but I'm new at Qt.
I am not versed in Lambdas, so this is what I tried:
bool bDoneStats = false; void doneStats(QNetworkReply *reply) { bDoneStats = true; // never gets here qDebug()<<"doneStats\n"; } void MainWindow::closeEvent(QCloseEvent *event) { QNetworkReply *reply; QString urlStr("https://foo.com/bar.php?some&data"); QUrl url(urlStr); if(!url.isValid()) { QString urlError = url.errorString(); qDebug() << "\n" << urlError << "\n"; } QNetworkRequest *request = new QNetworkRequest(url); request->setHeader(QNetworkRequest::ContentTypeHeader, "text/plain"); QByteArray content = urlStr; QNetworkAccessManager *nam = new QNetworkAccessManager; if(request != NULL && nam != NULL ) { reply = nam->post(*request, content); // also tried "SIGNAL(finished(QNetworkReply*))" connect(nam, SIGNAL(nam->finished(QNetworkReply*)), this, SLOT(doneStats(QNetworkReply*))); QNetworkReply::NetworkError netret = reply->error(); qDebug() << "\nSuccess " << url << " NetworkError " << netret << "\n"; } do {} while (!bDoneStats); // never breaks loop event->accept(); QMainWindow::closeEvent(event); }
It never reaches "doneStats", and goes into an endless loop. If I remove the do/while, it exits the function but nothing is written at the server.
What am I missing? I really appreciate your help.
-
@Sprezzatura said in Does QNetworkAccessManager::post() work in QMainWindow::closeEvent()?:
connect(nam, SIGNAL(nam->finished(QNetworkReply*)), this, SLOT(doneStats(QNetworkReply*)));
This looks wrong (signal part).
connect(nam, SIGNAL(finished(QNetworkReply*)), this SLOT(doneStats(QNetworkReply*)));
Or, since you are using Qt5 (I hope so):
connect(nam, &QNetworkAccessManager::finished, this, &MainWindow::doneStats);
-
@Sprezzatura said in Does QNetworkAccessManager::post() work in QMainWindow::closeEvent()?:
do {} while (!bDoneStats); // never breaks loop
When you get your code compiling (as per @Pl45m4's comment), what is this code about?? You won't want any such tight loop....
-
This is the revised code. It still does not call 'doneStats', and does not write to the server.
I am using Qt 5.12, because I need to be compatible with older versions of macOS.
void MainWindow::doneStats(QNetworkReply *reply) { bDoneStats = true; // member of MainWindow qDebug()<<"doneStats\n"; } void MainWindow::closeEvent(QCloseEvent *event) { bDoneStats = false; QNetworkReply *reply; const char *urlStr("https://foo.com/bar.php?some&data"); QUrl url(urlStr); if(!url.isValid()) { QString urlError = url.errorString(); qDebug() << "\n" << urlError << "\n"; } QNetworkRequest *request = new QNetworkRequest(url); request->setHeader(QNetworkRequest::ContentTypeHeader, "text/plain"); QByteArray content(urlStr); QNetworkAccessManager *nam = new QNetworkAccessManager; if(request != NULL && nam != NULL ) { reply = nam->post(*request, content); connect(nam, &QNetworkAccessManager::finished, this, &MainWindow::doneStats); QNetworkReply::NetworkError netret = reply->error(); // QNetworkReply::NoError qDebug() << "\nSuccess " << url << " NetworkError " << netret << "\n"; } event->accept(); QMainWindow::closeEvent(event); }
-
This post is deleted!
-
@JonB said in Does QNetworkAccessManager::post() work in QMainWindow::closeEvent()?:
@Sprezzatura said in Does QNetworkAccessManager::post() work in QMainWindow::closeEvent()?:
do {} while (!bDoneStats); // never breaks loop
When you get your code compiling (as per @Pl45m4's comment), what is this code about?? You won't want any such tight loop....
I am expecting 'bDoneStats' to be set to true by the callback or SLOT function, thus breaking the loop.
-
@Sprezzatura said in Does QNetworkAccessManager::post() work in QMainWindow::closeEvent()?:
I just realized I called 'connect' after the post, I will change the order and try again.
-
@Sprezzatura said in Does QNetworkAccessManager::post() work in QMainWindow::closeEvent()?:
I am expecting 'bDoneStats' to be set to true by the callback or SLOT function, thus breaking the loop.
This absolutely will not happen. Rather, your program will use up CPU time forever here, contributing to the entropy of The Universe :)
There are no multiple threads threads here. No signals or slots will fire while your loop executes. You will need a "wait for" event loop here instead, because you don't want to accept the original close event until this has completed. Look at QEventLoop::exec(). I don't know if there are consequences of calling this while inside
MainWindow::closeEvent()
. -
I've removed the do/while loop, I have re-ordered these statements:
connect(nam, &QNetworkAccessManager::finished, this, &MainWindow::doneStats); reply = nam->post(*request, content);
This is part of an application that otherwise runs fine. 'exec' is called in another part of the program (main.cpp).
The code works if invoked at the beginning of the program. It only seems to fail when it is called at the end of a session.
What do I need to do to get it to work?
-
Hi,
One way is to use a local QEventLoop to wait for the request to complete. However, does it really make sense to do it there ? It sounds rather like something that should be done in the main method at application end.
In any case, there's no need to allocate QNetworkRequest on the heap.
-
@Sprezzatura said in Does QNetworkAccessManager::post() work in QMainWindow::closeEvent()?:
'exec' is called in another part of the program (main.cpp).
I was talking about you putting an event loop here in your code:
QEventLoop loop; loop.exec();
That's how you have to do a "wait for", when you really need to, in Qt stuff.
-
@SGaist said in Does QNetworkAccessManager::post() work in QMainWindow::closeEvent()?:
Hi,
One way is to use a local QEventLoop to wait for the request to complete. However, does it really make sense to do it there ? It sounds rather like something that should be done in the main method at application end.
YES! That's one of the solutions I tried to do, but couldn't figure out where to do it. Where, in the code, can I intercept an application 'quit', prior to closeEvent?
-
OK, here is the final, working solution. I am new to Qt and am not familiar with QEventLoop. I needed to see how the whole thing fitted together in order to understand.
void MainWindow::closeEvent(QCloseEvent *event) { QNetworkReply *reply; QUrl url("https://foo.com/bar.php?some&data"); if(!url.isValid()) { QString urlError = url.errorString(); qDebug() << "\n" << urlError << "\n"; } QNetworkRequest *request = new QNetworkRequest(url); request->setHeader(QNetworkRequest::ContentTypeHeader, "text/plain"); QByteArray content = "https://foo.com/bar.php?some&data"; QNetworkAccessManager *nam = new QNetworkAccessManager; if(request != NULL && nam != NULL ) { QEventLoop loop; // https://www.codeproject.com/Questions/1196636/Qnetworkaccesmanager-doesn-t-emit-finished-signal connect (nam, SIGNAL(finished(QNetworkReply*)), &loop, SLOT(quit()), Qt::DirectConnection); reply = nam->post(*request, content); loop.exec(); QNetworkReply::NetworkError netret = reply->error(); qDebug() << "\nSuccess " << url << " NetworkError " << netret << "\n"; QList<QByteArray> headers = reply->rawHeaderList(); QList<QByteArray>::iterator hit; for(hit = headers.begin(); hit != headers.end(); hit++) // is empty if failed qDebug() << *hit << reply->rawHeader(*hit); } event->accept(); QMainWindow::closeEvent(event); }
-
@Sprezzatura
I think this looks better :)Your code as it stands presumably leaks
QNetworkRequest *request = new QNetworkRequest(url);
andQNetworkAccessManager *nam = new QNetworkAccessManager;
. You could put these on the stack (no pointers, nonew
). And I think you're supposed todeleteLater()
theQNetworkReply *reply = nam->post(*request, content);
.You can get away without, but one day when you want examine your code for memory leaks with e.g.
valgrind
it will be better.