头文件
class MachineUtils {
public:
MachineUtils();
~MachineUtils();
static QString GetComputerName();
static QString GetIP();
static QString GetMAC();
static QString GetDateTime();
static QString GetSystemType();
static QString GetSystem();
static QString GetYear();
static QString GetDeviceType();
static QString MachineId();
static QString createFileUniqueId();
};
实现
1.获取当前系统类型
/**
* @brief 获取当前系统类型
*
* @author
* @date 2021-02-21
*/
QString MachineUtils::GetSystemType()
{
QString strType;
#if defined(Q_OS_LINUX)
strType = "Linux";
#elif defined(Q_OS_WIN32)
strType = "Windows";
#elif defined(Q_OS_MAC)
strType = "MacOS";
#endif
return strType;
}
2.获取当前操作系统
/**
* @brief 获取当前操作系统
*
* @author
* @date 2021-02-21
*/
QString MachineUtils::GetSystem()
{
return QString("%1 %2").arg(QSysInfo::productType()).arg(QSysInfo::productVersion());
}
3.获取本地计算机名
/**
* @brief 获取本地计算机名
*
* @author
* @date 2021-02-21
*/
QString MachineUtils::GetComputerName()
{
return QHostInfo::localHostName();
}
4.获取本地计算机IP地址
/**
* @brief 获取本地计算机IP地址
*
* @author
* @date 2021-02-21
*/
QString MachineUtils::GetIP()
{
QString strIP;
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
int nListSize = ipAddressesList.size();
for (int i = 0; i < nListSize; ++i) {
if (ipAddressesList.at(i) != QHostAddress::LocalHost && ipAddressesList.at(i).toIPv4Address()) {
strIP = ipAddressesList.at(i).toString();
break;
}
}
// 如果没有找到,则以本地IP地址为IP
if (strIP.isEmpty())
strIP = QHostAddress(QHostAddress::LocalHost).toString();
return strIP;
}
5.获取本地计算机MAC地址
/**
* @brief 获取本地计算机MAC地址
*
* @author
* @date 2021-02-21
*/
QString MachineUtils::GetMAC()
{
QString strMacAdd;
QList<QNetworkInterface> nets = QNetworkInterface::allInterfaces(); // 获取所有网络接口列表
int nCnt = nets.count();
for (int i = 0; i < nCnt; i++) {
// 如果此网络接口被激活并且正在运行并且不是回环地址,则就是我们需要找的Mac地址
if (nets[i].flags().testFlag(QNetworkInterface::IsUp)
&& nets[i].flags().testFlag(QNetworkInterface::IsRunning)
&& !nets[i].flags().testFlag(QNetworkInterface::IsLoopBack)) {
strMacAdd = nets[i].hardwareAddress();
break;
}
}
return strMacAdd;
}
6.运用mac地址来计算硬件码
/**
* @brief 运用mac地址来计算硬件码
* md5后的mac地址
* @author
* @date 2021-02-21
*/
QString MachineUtils::MachineId()
{
QString md5;
QString macAddr = GetMAC();
QByteArray bb;
bb = QCryptographicHash::hash(macAddr.toLatin1(), QCryptographicHash::Md5);
md5.append(bb.toHex());
return md5;
}
7.获取当前CPU名
/**
* @brief 获取当前CPU名
*
* @author
* @date 2021-02-21
*/
QString MachineUtils::GetDeviceType()
{
QString cpuName;
#if defined(Q_OS_LINUX)
QString result = ProcessUitls::ExecuteLinuxCmd("cat /proc/cpuinfo|grep \"model name\"").simplified();
int index = result.lastIndexOf(":");
cpuName = result.mid(index + 2);
#elif defined(Q_OS_WIN32)
QSettings CPU("HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", QSettings::NativeFormat);
cpuName = CPU.value("ProcessorNameString").toString();
#elif defined(Q_OS_MAC)
cpuName = "";
#endif
return cpuName;
}
8.获取当前时间
/**
* @brief 获取当前时间
*
* @author
* @date 2021-02-21
*/
QString MachineUtils::GetDateTime()
{
return QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
}
9.获取当前年份
/**
* @brief 获取当前年份
*
* @author
* @date 2021-02-21
*/
QString MachineUtils::GetYear()
{
return QDateTime::currentDateTime().toString("yyyy");
}