qt常用代码段(获取ip,MAC,username,hostname)

// 涉及到网络模块时记得在工程问题,加上:   QT += network

// 并且根据相应提示,添加相应的头文件:#include <QtNetwork>

  1. // 获得ip地址  
  2. QString getIpAdress()  
  3. {  
  4.     QString localIPAddress = ”“;  
  5.      QList <QHostAddress>listAddress = QNetworkInterface::allAddresses();  
  6.      for(int j = 0; j < listAddress.size(); j++){  
  7.          if(!listAddress.at(j).isNull()  
  8.          && listAddress.at(j).protocol() ==  QAbstractSocket::IPv4Protocol  
  9.          && listAddress.at(j) != QHostAddress::LocalHost){  
  10.              localIPAddress = listAddress.at(j).toString();  
  11.              return localIPAddress;  
  12.          }  
  13.      }  
  14.      return localIPAddress;  
  15. }  
// 获得ip地址
QString getIpAdress()
{
    QString localIPAddress = "";
     QList <QHostAddress>listAddress = QNetworkInterface::allAddresses();
     for(int j = 0; j < listAddress.size(); j++){
         if(!listAddress.at(j).isNull()
         && listAddress.at(j).protocol() ==  QAbstractSocket::IPv4Protocol
         && listAddress.at(j) != QHostAddress::LocalHost){
             localIPAddress = listAddress.at(j).toString();
             return localIPAddress;
         }
     }
     return localIPAddress;
}


  1. //获得MAC地址  
  2. QStringUSBMainUI::getMACAdress()  
  3. {  
  4.     QList<QNetworkInterface> NetList;//网卡链表  
  5.     int NetCount = 0;//网卡个数  
  6.     int Neti=0;  
  7.     QNetworkInterface thisNet;//所要使用的网卡  
  8.       
  9.     NetList = QNetworkInterface::allInterfaces();//获取所有网卡信息  
  10.     NetCount = NetList.count();//统计网卡个数  
  11.       
  12.     for(Neti = 0; Neti < NetCount; Neti++){//遍历所有网卡  
  13.         if( NetList[Neti].isValid() ){//判断该网卡是否是合法  
  14.             thisNet = NetList[Neti];//将该网卡置为当前网卡  
  15.             break;  
  16.         }  
  17.     }  
  18.     return(thisNet.hardwareAddress());//获取该网卡的MAC  
  19. }  
//获得MAC地址
QStringUSBMainUI::getMACAdress()
{
    QList<QNetworkInterface> NetList;//网卡链表
    int NetCount = 0;//网卡个数
    int Neti=0;
    QNetworkInterface thisNet;//所要使用的网卡

    NetList = QNetworkInterface::allInterfaces();//获取所有网卡信息
    NetCount = NetList.count();//统计网卡个数

    for(Neti = 0; Neti < NetCount; Neti++){//遍历所有网卡
        if( NetList[Neti].isValid() ){//判断该网卡是否是合法
            thisNet = NetList[Neti];//将该网卡置为当前网卡
            break;
        }
    }
    return(thisNet.hardwareAddress());//获取该网卡的MAC
}

  1. // 获得用户名  
  2. QString getUserName()  
  3. {  
  4.     QString userName = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);  
  5.     userName = userName.section(“/”, -1, -1);  
  6.     return userName;  
  7. }  
// 获得用户名 
QString getUserName()
{
QString userName = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
userName = userName.section("/", -1, -1);
return userName;
}

  1. // 获得机器名字  
  2. QString getMachineName()  
  3. {  
  4.     QString localHostName = QHostInfo::localHostName();  
  5.     return localHostName;  
  6. }  
// 获得机器名字 
QString getMachineName()
{
QString localHostName = QHostInfo::localHostName();
return localHostName;
}



//转 秋叶原 && Mike || 麦克

猜你喜欢

转载自blog.csdn.net/a119258/article/details/78119761
今日推荐