蜜桃成熟时3之蜜桃仙子:【2011.03.16】QT获取本地ip

来源:百度文库 编辑:九乡新闻网 时间:2024/05/03 10:54:30
bool GetLocalIP(QString & IP)
{      QList mList=QNetworkInterface::allInterfaces();      if(mList.count()>0)    
      {        
            foreach(QNetworkInterface inf,mList)        
            {            
                  QList  eList=inf.addressEntries();                                  foreach(QNetworkAddressEntry ent,eList)            
                 {                
                        QHostAddress ha=ent.ip();                
                        if(!ha.isNull()&&ha.protocol()==QAbstractSocket::IPv4Protocol)                                          {                    
                              IP=ha.toString();                  
                              break;                
                       }            
           }            
           if(IP.isEmpty()||IP=="")               continue;            
           else                break;        
           }        
           if(IP.isEmpty()||IP=="")           return false;        
           else            return true;    
      }    
      else          {              return false;          }
}