How can I monitor internet connection status in qt? What I mean is that I'd like to be able to change icon to active/inactive depend on internet connection being present or not.
Asked
Active
Viewed 5,275 times
3
-
What determines whether a connection is "present" or not? – Joe Dec 08 '12 at 17:16
-
1@Joe just for you, connection is present if I'm connected to the internet, and is not present if I'm not connected. Does that make sense? – smallB Dec 08 '12 at 17:26
-
Well, you can tell if you have an IP address without much trouble. Connected to the internet is harder, this means you'd have to see if you can reach some external site from where you are. That's why I asked -- do you need to see if you have an active network connection, or are actually on a network that can reach the internet? – Joe Dec 08 '12 at 17:51
-
@Joe I'm sorry but what according to you "Monitoring of internet connection status" means? C'mon, don't be a jerk. – smallB Dec 09 '12 at 09:56
-
Nobody's being a jerk here. Just saying, depending on how YOU define "monitoring an internet connection" means the answer varies. – Joe Dec 09 '12 at 13:52
-
2@Joe Monitoring internet connection status - what internet connection status may be? – smallB Dec 09 '12 at 16:06
3 Answers
1
If you are using QML, there is NetworkInfo QML element from Qt Mobility package. Also, that contains an example, how to check is WLAN connection present.

hate-engine
- 2,300
- 18
- 26
0
I use this code to check the internet status.
bool ConnectivityManager::isOnline()
{
bool retVal = false;
QNetworkAccessManager nam;
QNetworkRequest req(QUrl("http://www.google.com"));
QNetworkReply* reply = nam.get(req);
QEventLoop loop;
QTimer timeoutTimer;
connect(&timeoutTimer, SIGNAL(timeout()), &loop, SLOT(quit()));
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
timeoutTimer.setSingleShot(true);
timeoutTimer.start(3000);
loop.exec();
if (reply->bytesAvailable())
{
retVal = true;
}
return retVal;
}

Jitesh Prajapati
- 2,533
- 4
- 29
- 51

Winston Rodrigues
- 63
- 6
-
yeah but might not be the ideal solution pinging goolge for for detecting a working internet connection, your users will have to verify that they are not a bot on every google search afterwards. https://forum.qt.io/topic/98676/how-to-check-whether-the-computer-connects-the-internet – HBatalha Feb 10 '21 at 09:57
-
This is a pretty bad idea. Either Qt needs to add this to their library, or you can use the solution posted in the Qt forum above. – Alex Baum Dec 11 '21 at 23:34
0
I've had an issue using a solution similar to the Winston Rodrigues one and Flatpak. This solution was working on almost all computers I have tested but one. Internet connection was not detected for unclear reason (no error was raised). So, here is the solution that works for me in all situations, so far:
bool InternetIsOn()
{
QUrl host("1.1.1.1"); // put here URL (or IP) of your own website if you have one
qDebug() << tr("Checking internet connection: connecting to %1 ...").arg(host.toString());
QTcpSocket* sock = new QTcpSocket(this);
connect(sock, &QTcpSocket::errorOccurred, this, [](QAbstractSocket::SocketError error) {
qDebug() << "This error occured during Internet checking:" << error;
});
sock->connectToHost(host.toString(), 80);
bool connected = sock->waitForConnected(3'000);//ms
if (!connected)
{
sock->abort();
qDebug() << tr("%1 is not reachable. Not connected to the Internet").arg(host.toString());
return false;
}
sock->close();
qDebug() << tr("Connected to the Internet");
return true;
}

Pamputt
- 173
- 1
- 11