• Ubuntu中基于QT的系统网线连接状态的实时监视


    1.必要准备

             需包: #include <QNetworkInterface>

    2.实现获取当前的网线连接状态

           以下是自己在网络上搜到的一个解决方法,且没有加入iface.flags().testFlag(QNetworkInterface::IsRunning) 这一逻辑判断,经测试实时性极不可靠,在虚拟机里调试时发现有时候有10s左右的延迟,但运行在嵌入式系统上时几乎就是一摆设。于是自己阅读QT帮助尝试了多种方法并加以改造,发现加入了第二项条件判断后,结果就非常的可靠了。

    [cpp] view plain copy
     
    1. bool isConnectedToNetwork()  
    2. {  
    3.     QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces();  
    4.     bool isConnected = false;  
    5.   
    6.     for (int i = 0; i < ifaces.count(); i++)  
    7.     {  
    8.         QNetworkInterface iface = ifaces.at(i);  
    9.         if ( iface.flags().testFlag(QNetworkInterface::IsUp)  
    10.              && iface.flags().testFlag(QNetworkInterface::IsRunning)  
    11.              && !iface.flags().testFlag(QNetworkInterface::IsLoopBack)  
    12.               )  
    13.         {  
    14.   
    15.             // this loop is important  
    16.             for (int j=0; j<iface.addressEntries().count(); j++)  
    17.             {  
    18.                 // we have an interface that is up, and has an ip address  
    19.                 // therefore the link is present  
    20.   
    21.                 // we will only enable this check on first positive,  
    22.                 // all later results are incorrect  
    23.                 if (isConnected == false)  
    24.                     isConnected = true;  
    25.             }  
    26.         }  
    27.   
    28.     }  
    29.   
    30.     return isConnected;  
    31. }  

    3. 实时性监测实现

        实时性的实现是利用了QT的定时器和信号与槽机制。这个大家甚至比我都清楚,就不啰嗦了。

    转自:http://blog.csdn.net/u010492096/article/details/40110749

  • 相关阅读:
    hdu 1084(水题)
    hdu 1872(稳定排序)
    NOI 2008 志愿者招募 / bzoj 1061 (最小费用最大流)
    hdu 1019(LCM)
    hdu 1876(dp)
    Codeforces Round #171 (Div. 2)
    poj 3422(最小费用最大流)
    poj 3264(简单线段树)
    Codeforces Round #156 (Div. 2)
    Codeforces Round #169 (Div. 2)
  • 原文地址:https://www.cnblogs.com/liushui-sky/p/6479466.html
Copyright © 2020-2023  润新知