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 4,402 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
2 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,260
- 17
- 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,661
- 4
- 28
- 47
Winston Rodrigues
- 53
- 5
-
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