QT入门系列(8):获取IP地址、MAC地址、操作系统版本/位数

版权声明:本文为@小疯 原创文章,如需转载,请注明出处! https://blog.csdn.net/qq598535550/article/details/63692361

一、获取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;
}

二、获得MAC地址

QString 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
}

三、获取Windows操作系统版本

QString getWindowsVersion()
{
    QString winVersion = "";
    QSysInfo::WinVersion wv = QSysInfo::windowsVersion();
    switch (wv) {
    case QSysInfo::WV_XP:
        winVersion = "Windows XP";
        break;
    case QSysInfo::WV_2003:
        winVersion = "Windows Server 2003";
        break;
    case QSysInfo::WV_VISTA:
        winVersion = "Windows Vista";
        break;
    case QSysInfo::WV_WINDOWS7:
        winVersion = "Windows 7";
        break;
    case QSysInfo::WV_WINDOWS8:
        winVersion = "Windows 8";
        break;
    case QSysInfo::WV_WINDOWS8_1:
        winVersion = "Windows 8.1";
        break;
    case QSysInfo::WV_WINDOWS10:
        winVersion = "Windows 10";
        break;
    default:
        break;
    }
    return winVersion;
}

四、获取操作系统位数

int getSystemBit()
{
    return QSysInfo::WordSize;
}

五、调用演示

qDebug() << getIpAdress();
qDebug() << getMacAdress();
qDebug() << getWindowsVersion();
qDebug() << getSystemBit();

输出结果:
“10.20.72.87”
“B8:CA:3A:96:FE:75”
“Windows 7”
64

猜你喜欢

转载自blog.csdn.net/qq598535550/article/details/63692361