Checking if a device has an active internet connection
-
@Cobra91151 Absolutely! But what happens when
google.com
goes out of business? ;-)In such case he can use his own website or other websites, for example:
"1.1.1.1" (cloudflare)
instead of"google.com"
. :) -
In such case he can use his own website or other websites, for example:
"1.1.1.1" (cloudflare)
instead of"google.com"
. :)@Cobra91151
CloudFlare is now a Google Cloud Platform Technology Partner So they will be brought down with Google :D -
@Cobra91151
CloudFlare is now a Google Cloud Platform Technology Partner So they will be brought down with Google :DOk, good point. I would suggest to use one of the
Public DNS Server
from this list: https://public-dns.info/ ;-) -
#include <QCoreApplication> #include <QNetworkInformation> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); if ( QNetworkInformation::loadDefaultBackend() && QNetworkInformation::loadBackendByFeatures( QNetworkInformation::Features::Reachability ) ) { QNetworkInformation* net_info = QNetworkInformation::instance(); if ( nullptr != net_info ) { if(net_info->reachability() == QNetworkInformation::Reachability::Online) { qDebug() << "Device is Online"; } else { qDebug() << "Device is offline"; } } } return a.exec(); }
-
@RaptaG
It provides all the (non-UI) services Qt requires/offers (signals, slots, lots & lots of other stuff, initializations)... basically a Qt program won;t work without one, else it's a not a program actually using Qt. -
@JonB Can I borrow your code and use it on my open-source project (GPL3)? I will credit you.
-
@RaptaG
I don't know what code you are thinking of and not sure what you could want? But you are welcome to whatever, no credit necessary. -
Something else now, if I use it in a bool function, then
return a.exec()
is not necessary, right?Also, how likely is it for the first 2
if
statements not to be the case?@RaptaG said in Checking if a device has an active internet connection:
return a.exec() is not necessary, right?
Wrong.
If you're writing a Qt application which requires the event loop to be running (most Qt applications) then you have to call exec()... -
@RaptaG said in Checking if a device has an active internet connection:
return a.exec() is not necessary, right?
Wrong.
If you're writing a Qt application which requires the event loop to be running (most Qt applications) then you have to call exec()... -
@RaptaG
I really don't think we know what you are asking about here.There is nothing to stop you calling
exec()
in a function, boolean or not.bool foo() { int unusedReturnResult = someApplicationObject.exec(); // ignore that return result return false; // whatever boolean value you want }
However, since
exec()
is the entry point to running the main Qt application event loop you are only going to want to call it once (and it does not exit till Qt quits event loop). So I cannot imagine what/why you want to move it to some function which returns a boolean. Makes me thing you are going to use it oddly. If you are thinking of calling this each time you want to check "if a device has an active internet connection" this is not the architecture. -
@RaptaG
I really don't think we know what you are asking about here.There is nothing to stop you calling
exec()
in a function, boolean or not.bool foo() { int unusedReturnResult = someApplicationObject.exec(); // ignore that return result return false; // whatever boolean value you want }
However, since
exec()
is the entry point to running the main Qt application event loop you are only going to want to call it once (and it does not exit till Qt quits event loop). So I cannot imagine what/why you want to move it to some function which returns a boolean. Makes me thing you are going to use it oddly. If you are thinking of calling this each time you want to check "if a device has an active internet connection" this is not the architecture. -
@JonB said in Checking if a device has an active internet connection:
If you are thinking of calling this each time you want to check "if a device has an active internet connection"
That's what I was planning to do tbh 😅
@RaptaG said in Checking if a device has an active internet connection:
That's what I was planning to do tbh
Why? You will block your application. Qt is an asynchronous framework - you should use it as such. QTcpSocket is also asynchronous...
-
@RaptaG said in Checking if a device has an active internet connection:
That's what I was planning to do tbh
Why? You will block your application. Qt is an asynchronous framework - you should use it as such. QTcpSocket is also asynchronous...
-
@jsulm I am a newbie and async stuff look really difficult. Even then, I only need to check for internet connection very few times/day in my program
@RaptaG It is not that difficult and you should really learn it, else there is no point in using Qt.
QTcpSocket has a signal: https://doc.qt.io/qt-6/qabstractsocket.html#connected
Just connect a slot to it and if this slot is called you're connected. -
@RaptaG It is not that difficult and you should really learn it, else there is no point in using Qt.
QTcpSocket has a signal: https://doc.qt.io/qt-6/qabstractsocket.html#connected
Just connect a slot to it and if this slot is called you're connected. -
@jsulm I want to learn to use Qt properly, because I want to implement a GUI for my C++ project with it. Where eould suggest me to start learning Qt, considering I am a C++ beginner?
@RaptaG You can start with the basic example applications https://doc.qt.io/qt-6/qtexamplesandtutorials.html