Solved QNetworkAccessManager connection loss
-
I just noticed something while using
QNetworkAccessManager
. I made an application that loops over a list of urls and request usingQNetworkAccessManager
, my internet connection wasn't good that day and was oscillating, like closing the connection all of the sudden, everywhere, even when using the browser, when I tried to connect to a website it was partially downloaded and then it just stop loading, sometimes the images wasn't full downloaded or not at all.The thing with
QNetworkAccessManager
that I noticed is that it doesn't detect connection loss at all, after having this issue with the oscillating internet connection I decided to make a test again, now with a good connection, to unplug the internet cable to see the Qt's reaction, and... nothing happened, it just freezes on the last request and never calls the next one (due to timeout, idk) or not even show me an error.Can anyone confirm that for me? Or if it's confirmed already, is there a way to solve that problem?
-
Hi,
Did you connect the error signal ?
-
Yes, but it doesn't emit when I unplug the internet cable, nothing happens. I wanted for like 30 minutes in order to see if something happens and nop.
-
What version of Qt are you using ?
On what platform ? -
Qt 5.10.1, MSVC 2015, Windows 10.
-
@Mr-Gisa said in QNetworkAccessManager connection loss:
Yes, but it doesn't emit when I unplug the internet cable,
what happens the opposite way, I mean, with cable disconnect if you start your app does the QNAM error signal got fired?
-
Hi
Just as a note.QNetworkConfigurationManager manager; pManager.setConfiguration(manager.defaultConfiguration()); connect(&pManager, SIGNAL(networkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility)), this, SLOT(networkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility)));
Allowed me to detect if wifi was turned off.
With QNetworkAccessManager, i would have to write something to have it detect connection was lost.
Not sure you situations is the same but no harm in mentioning QNetworkConfigurationManager
-
@Pablo-J-Rogina I only get
HostNotFoundError
.@mrjj It didn't work for me, it just stops and shows nothing more.
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { manager.setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy); QNetworkConfigurationManager config; manager.setConfiguration(config.defaultConfiguration()); connect(&manager, &QNetworkAccessManager::networkAccessibleChanged, this, &MainWindow::networkAccessibleChanged); for (int i = 0; i < 200; ++i) { auto reply = manager.get(QNetworkRequest(QUrl("https://www.google.net/"))); connect(reply, &QNetworkReply::finished, this, &MainWindow::handle); connect(reply, QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error), this, &MainWindow::error); } } void MainWindow::handle() { auto reply = qobject_cast<QNetworkReply *>(sender()); reply->deleteLater(); auto status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); qDebug() << status; } void MainWindow::error(QNetworkReply::NetworkError error) { qDebug() << error; } void MainWindow::networkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility accessibility) { qDebug() << accessibility; }
-
@Mr-Gisa said in QNetworkAccessManager connection loss:
QNetworkConfigurationManager config;
And its not just because it runs out of scope before signal is actually emitted ?
Try to have it as a member.
It worked on both windows and linux for me.Also be aware that
http://doc.qt.io/qt-5/qnetworkconfiguration.html#details
have types. so you might need to use QNetworkConfiguration::BearerEthernet
also use
http://doc.qt.io/qt-5/qnetworkconfigurationmanager.html#allConfigurations
to see what you have.
If you have both lan/wifi and more as possible access points.it might not be defaultConfiguration() you want to monitor.
-
How to monitor whatever the user is using, if it's wifi or ethernet? I tried and I could find anything.
I want to emit an error message to the user when the connection is lost.
-
@Mr-Gisa From what @mrjj wrote:
"http://doc.qt.io/qt-5/qnetworkconfigurationmanager.html#allConfigurations
to see what you have."