Qt World Summit: Register Today!

How to check internet connection in Qt

  • Hi all,,
    @bool networkInterface::networkAvailable()
    QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces();
    bool result = false;

    for (int i = 0; i < ifaces.count(); i++)
        QNetworkInterface iface = ifaces.at(i);
        if ( iface.flags().testFlag(QNetworkInterface::IsUp)
             && !iface.flags().testFlag(QNetworkInterface::IsLoopBack) )
            for (int j=0; j<iface.addressEntries().count(); j++)
                if (result == false)
                    result = true;
    return result;


    this code is checking internet and working correctly in windows,,,but if i disconnect the internet in ubuntu means still the online status is true,,Please give me a suggesstion,,

  • Hi saravanavelu39, How are you friend? :)
    I have a simple solution :)) very simple!!!
    just ping ip and check output...
    Look at this:
    @QProcess pingProcess;
    QString exec = "ping";
    QStringList params;
    params << "-c" << "1" << IP;
    QString p_stdout = pingProcess.readAllStandardOutput();
    QString p_stderr = pingProcess.readAllStandardError();@

  • Hi Jafar,,
    Thanks for your reply,,,I used your code,,that is working correctly in ubuntu,,but the application some time hangs after using this code,,so could you please give me a some other suggesstion,,


  • of course saravanavelu..
    There are some solution in this link
    i hope be useful :)

Log in to reply