Verfiying the network connection using Qt 4.4

后端 未结 3 1966
后悔当初
后悔当初 2020-12-17 01:34

I have an application which runs a tool that requires network connection. Now my aim is to check whether the user has a network connection, if he don\'t have one, i can stra

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-17 02:08

    this code will help you.

    #include 
    #include 
    
    bool isConnectedToNetwork()
    {
        QList 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) )
            {
    
    #ifdef DEBUG
                // details of connection
                qDebug() << "name:" << iface.name() << endl
                        << "ip addresses:" << endl
                        << "mac:" << iface.hardwareAddress() << endl;
    #endif
    
                // this loop is important
                for (int j=0; j

提交回复
热议问题