头文件

  1. class MachineUtils {
  2. public:
  3. MachineUtils();
  4. ~MachineUtils();
  5. static QString GetComputerName();
  6. static QString GetIP();
  7. static QString GetMAC();
  8. static QString GetDateTime();
  9. static QString GetSystemType();
  10. static QString GetSystem();
  11. static QString GetYear();
  12. static QString GetDeviceType();
  13. static QString MachineId();
  14. static QString createFileUniqueId();
  15. };

实现

1.获取当前系统类型

  1. /**
  2. * @brief 获取当前系统类型
  3. *
  4. * @author
  5. * @date 2021-02-21
  6. */
  7. QString MachineUtils::GetSystemType()
  8. {
  9. QString strType;
  10. #if defined(Q_OS_LINUX)
  11. strType = "Linux";
  12. #elif defined(Q_OS_WIN32)
  13. strType = "Windows";
  14. #elif defined(Q_OS_MAC)
  15. strType = "MacOS";
  16. #endif
  17. return strType;
  18. }

2.获取当前操作系统

  1. /**
  2. * @brief 获取当前操作系统
  3. *
  4. * @author
  5. * @date 2021-02-21
  6. */
  7. QString MachineUtils::GetSystem()
  8. {
  9. return QString("%1 %2").arg(QSysInfo::productType()).arg(QSysInfo::productVersion());
  10. }

3.获取本地计算机名

  1. /**
  2. * @brief 获取本地计算机名
  3. *
  4. * @author
  5. * @date 2021-02-21
  6. */
  7. QString MachineUtils::GetComputerName()
  8. {
  9. return QHostInfo::localHostName();
  10. }

4.获取本地计算机IP地址

  1. /**
  2. * @brief 获取本地计算机IP地址
  3. *
  4. * @author
  5. * @date 2021-02-21
  6. */
  7. QString MachineUtils::GetIP()
  8. {
  9. QString strIP;
  10. QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
  11. int nListSize = ipAddressesList.size();
  12. for (int i = 0; i < nListSize; ++i) {
  13. if (ipAddressesList.at(i) != QHostAddress::LocalHost && ipAddressesList.at(i).toIPv4Address()) {
  14. strIP = ipAddressesList.at(i).toString();
  15. break;
  16. }
  17. }
  18. // 如果没有找到,则以本地IP地址为IP
  19. if (strIP.isEmpty())
  20. strIP = QHostAddress(QHostAddress::LocalHost).toString();
  21. return strIP;
  22. }

5.获取本地计算机MAC地址

  1. /**
  2. * @brief 获取本地计算机MAC地址
  3. *
  4. * @author
  5. * @date 2021-02-21
  6. */
  7. QString MachineUtils::GetMAC()
  8. {
  9. QString strMacAdd;
  10. QList<QNetworkInterface> nets = QNetworkInterface::allInterfaces(); // 获取所有网络接口列表
  11. int nCnt = nets.count();
  12. for (int i = 0; i < nCnt; i++) {
  13. // 如果此网络接口被激活并且正在运行并且不是回环地址,则就是我们需要找的Mac地址
  14. if (nets[i].flags().testFlag(QNetworkInterface::IsUp)
  15. && nets[i].flags().testFlag(QNetworkInterface::IsRunning)
  16. && !nets[i].flags().testFlag(QNetworkInterface::IsLoopBack)) {
  17. strMacAdd = nets[i].hardwareAddress();
  18. break;
  19. }
  20. }
  21. return strMacAdd;
  22. }

6.运用mac地址来计算硬件码

  1. /**
  2. * @brief 运用mac地址来计算硬件码
  3. * md5后的mac地址
  4. * @author
  5. * @date 2021-02-21
  6. */
  7. QString MachineUtils::MachineId()
  8. {
  9. QString md5;
  10. QString macAddr = GetMAC();
  11. QByteArray bb;
  12. bb = QCryptographicHash::hash(macAddr.toLatin1(), QCryptographicHash::Md5);
  13. md5.append(bb.toHex());
  14. return md5;
  15. }

7.获取当前CPU名

  1. /**
  2. * @brief 获取当前CPU名
  3. *
  4. * @author
  5. * @date 2021-02-21
  6. */
  7. QString MachineUtils::GetDeviceType()
  8. {
  9. QString cpuName;
  10. #if defined(Q_OS_LINUX)
  11. QString result = ProcessUitls::ExecuteLinuxCmd("cat /proc/cpuinfo|grep \"model name\"").simplified();
  12. int index = result.lastIndexOf(":");
  13. cpuName = result.mid(index + 2);
  14. #elif defined(Q_OS_WIN32)
  15. QSettings CPU("HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", QSettings::NativeFormat);
  16. cpuName = CPU.value("ProcessorNameString").toString();
  17. #elif defined(Q_OS_MAC)
  18. cpuName = "";
  19. #endif
  20. return cpuName;
  21. }

8.获取当前时间

  1. /**
  2. * @brief 获取当前时间
  3. *
  4. * @author
  5. * @date 2021-02-21
  6. */
  7. QString MachineUtils::GetDateTime()
  8. {
  9. return QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
  10. }

9.获取当前年份

  1. /**
  2. * @brief 获取当前年份
  3. *
  4. * @author
  5. * @date 2021-02-21
  6. */
  7. QString MachineUtils::GetYear()
  8. {
  9. return QDateTime::currentDateTime().toString("yyyy");
  10. }