Solved QNetworkAccessManager connection loss
-
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."