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 || 麥克

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章