I'm trying to perform a network connectivity check whenever user opens the app or whenever app comes in foreground. Below is the sample code
void ApplicationUI::onFullscreen()
{
qDebug()<<"Application has entered foreground";
QNetworkConfigurationManager mgr;
QList<QNetworkConfiguration> activeConfigs = mgr.allConfigurations(QNetworkConfiguration::Active);
if (activeConfigs.count() > 0)
{
qDebug()<<"Has Internet connection";
}
else
{
qDebug()<<"No Internet connection";
}
}
This always prints Has Internet connection even when the network connection is off. Any ideas?
You can use QNetworkConfigurationManager.isOnline().
QNetworkConfigurationManager mgr;
mgr.isOnline();
If you want to get notified about changes of the online state then you also can connect to the QNetworkConfigurationManager::onlineStateChanged(bool isOnline) signal.
connect(mgr, SIGNAL(onlineStateChanged(bool)), this, SLOT(onOnlineStateChanged(bool)));