How to Check Internet Connection Availability Using Qt/C++

The one and only correct answer is, you have to check if you can access some famous (like Google or Facebook) server on the internet. This question comes up a lot and surprisingly enough there is no clear answer for it because being connected to the internet can have different meanings in different countries. For example in my home country of Iran (and in China and some other countries too), Facebook and some other popular websites are forbidden under the law, so being connected to the Internet does not mean being connected to Facebook, and vice versa. So, you have to use a website which is not forbidden in the country your app is going to be used, and then use the simple approach below:



As you can see in the example code below, I simply have created a QNetworkAccessManager to send a request to Google, but be careful, even Google might be banned in some countries.

QNetworkAccessManager nam;
QNetworkRequest req(QUrl("http://www.google.com"));
QNetworkReply* reply = nam.get(req);
QEventLoop loop;
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
if (reply->bytesAvailable())
	QMessageBox::information(this, "Info", "You are connected to the internet :)");
else
	QMessageBox::critical(this, "Info", "You are not connected to the internet :(");

Don’t forget to add the following to your list of incudes:

#include <QtWidgets>
#include <QtNetwork>

Also you have to add network module to your Project .PRO file:

QT += network

This simple piece of code can be used in a function to tell you if you are connected to the internet or not. As you can see, Available Bytes will tell you your connection’s status.



5 Replies to “How to Check Internet Connection Availability Using Qt/C++”

  1. Thank you, the code works fine for me. However, if the PC has a connection availed but without internet. For instance, you turn on the wifi and disconnect the cable from the router. The code will stuck on loop. exec();
    I try both versions of the code, and the problem remains.

    1. I’ve never faced or tried this case for myself but it should time out at some point.
      If you want to be able to set a timeout yourself, you can modify the code and use something like this:


      QEventLoop loop;
      QTimer timeoutTimer;
      connect(&timeoutTimer, SIGNAL(timeout()), &loop, SLOT(quit));
      connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
      timeoutTimer.setSingleShot(true);
      timeoutTimer.start(5000); // this is your timeout in milliseconds
      loop.exec();

  2. hello, thanks for this,

    do you have an updated code for the latest QT?
    it looks like this doesn’t work on QT 5.10 , because the loop gets stuck on loop.exec();

    it seems that the finished signal is called before I use loop.exec, but this might be because I already have a QT loop going on for my application…

    1. Hmmm, this is quite strange since I already use it with the latest Qt version. I guess you have a super fast returning reply.
      Try the following instead:


      QNetworkAccessManager nam;
      QNetworkRequest req(QUrl("http://www.google.com"));
      QNetworkReply *reply = nam.get(req);
      QEventLoop loop;
      QObject::connect(reply, SIGNAL(readyRead()), &loop, SLOT(quit()));
      QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
      if(!reply->isFinished())
      loop.exec();
      if(reply->bytesAvailable())
      qDebug() << "You are connected to the internet :)"; else qDebug() << "You are not connected to the internet :(";

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.