Qt networkAccessibleChanged signal not firing

时间:2017-06-15 10:14:46

标签: c++ qt

My configuration : Windows, QT 5.5.1

I want to monitor the network state (that I simulate by plugging/unpluggin the network cable).

This is my code:

mNetManager = new QNetworkAccessManager(this);

connect(mNetManager, &QNetworkAccessManager::networkAccessibleChanged,
        this, &NetworkTester::updateFlag);

The method updateFlag is never called because the signal QNetworkAccessManager::networkAccessibleChanged is never fired.

I got around this by setting a timer :

QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(checkNetwork()));
    timer->start(1000);

void NetworkTester::checkNetwork()
{
switch (mNetManager->networkAccessible()) {
case QNetworkAccessManager::Accessible:
    qDebug() << "You are online.";
    break;

case QNetworkAccessManager::NotAccessible:
    qDebug() << "You are offline.";
    break;

case QNetworkAccessManager::UnknownAccessibility:
default:
    qDebug() << "You know nothing, Jon Snow.";
    break;
}

}

But I can't seem to detect any change as it always returns QNetworkAccessManager::networkAccessible when I plug/unplug network cable.

Any ideas on what I might be missing? Thanks.

1 个答案:

答案 0 :(得分:0)

QNetworkConfigurationManager is the class you are looking for. Take a look at onlineStateChanged(bool isOnline) signal.