一、获取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
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
二、获得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}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
三、获取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;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
四、获取操作系统位数
int getSystemBit(){ return QSysInfo::WordSize;}
- 1
- 2
- 3
- 4
五、调用演示
qDebug() << getIpAdress();qDebug() << getMacAdress();qDebug() << getWindowsVersion();qDebug() << getSystemBit();
- 1
- 2
- 3
- 4
输出结果:
“10.20.72.87”
“B8:CA:3A:96:FE:75”
“Windows 7”
64
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.youkuaiyun.com/jiangjunshow