1
我試圖在OS X Yosemite 10.10.3上用Qt 5.4檢查互聯網連接狀態,但每次運行代碼時都會連接結果,儘管我沒有連接任何東西。如何使用Qt 5.4在OS X Yosemite 10.10.3上檢測Internet連接狀態?
bool NetworkAccessManager::currentConnectivityStatus()
{
QNetworkAccessManager *namanager = new QNetworkAccessManager(this);
switch (namanager->networkAccessible()) {
case QNetworkAccessManager::UnknownAccessibility:
kdebug() << Q_FUNC_INFO << "QNetworkAccessManager::UnknownAccessibility";
break;
case QNetworkAccessManager::NotAccessible:
kdebug() << Q_FUNC_INFO << "QNetworkAccessManager::NotAccessible";
break;
case QNetworkAccessManager::Accessible:
kdebug() << Q_FUNC_INFO << "QNetworkAccessManager::Accessible";
break;
default:
break;
}
QNetworkConfigurationManager manager;
kdebug()<< Q_FUNC_INFO << " network connection " << manager.isOnline();
QNetworkConfiguration cfg = manager.defaultConfiguration();
QNetworkSession *session = new QNetworkSession(cfg);
session->open();
session->waitForOpened();
if(session->isOpen())
{
switch (session->state()) {
case QNetworkSession::Connected:
kdebug() << Q_FUNC_INFO << "QNetworkSession::Connected";
break;
case QNetworkSession::Disconnected:
kdebug() << Q_FUNC_INFO << "QNetworkSession::disconnect";
break;
default:
break;
}
}
}
這裏是所有上述的結果,當我斷開一切
DEBUG 2015-04-25T16:00:59.560 bool NetworkAccessManager::currentConnectivityStatus() QNetworkAccessManager::Accessible
DEBUG 2015-04-25T16:00:59.560 bool NetworkAccessManager::currentConnectivityStatus() network connection OK true
DEBUG 2015-04-25T16:00:59.560 bool NetworkAccessManager::currentConnectivityStatus() QNetworkSession::Connected
您應該嘗試ping外部服務器。 –
我檢測到從NetworkReply到我請求的連接狀態,但我正在尋找另一種方法來檢測它,而不執行NetworkRequest –