Qt系统读取

本文展示了如何使用C++和Qt库获取系统的编译信息、CPU架构、主机名、IP地址、MAC地址、CPU型号、显卡信息以及网络连接状态,通过实例代码展示了如何检查本地计算机的网络连通性。
摘要由CSDN通过智能技术生成

系统读取

功能

#include <QString>
#include <QStringList>
#include <QSize>
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif

// 系统基础信息
enum SysBase {
    buildAbi,               // 编译Qt程序的平台的架构全信息
    buildCpuArchitecture,   // 编译Qt程序的CPU架构
    currentCpuArchitecture, // 当前所运行在的CPU架构
    kernelType,             // 内核类型
    kernelVersion,          // 内核版本
    machineHostName,        // 主机名称
    prettyProductName,      // 操作系统的产品名称
    productType,            // 系统名称
    productVersion,         // 产品版本
};

// 获取系统信息
#include <QCoreApplication>
#include <QSysInfo>
#include <QDebug>
QStringList SysBase()
{
    QStringList l_SystemInfo;
//    l_SystemInfo << QSysInfo::WindowsVersion;
    l_SystemInfo << QSysInfo::buildAbi();
    l_SystemInfo << QSysInfo::buildCpuArchitecture();
    l_SystemInfo << QSysInfo::currentCpuArchitecture();
    l_SystemInfo << QSysInfo::kernelType();     //内核类型
    l_SystemInfo << QSysInfo::kernelVersion();    //内核版本
    l_SystemInfo << QSysInfo::machineHostName();   // 主机名称
    l_SystemInfo << QSysInfo::prettyProductName();   //操作系统的产品名称
    l_SystemInfo << QSysInfo::productType();      // 系统名称
    l_SystemInfo << QSysInfo::productVersion();  //产品版本

//    qInfo() << "WindowsVersion: " << QSysInfo::WindowsVersion;
//    qInfo() << "buildAbi: " << QSysInfo::buildAbi();
//    qInfo() << "buildCpuArchitecture: " << QSysInfo::buildCpuArchitecture();
//    qInfo() << "currentCpuArchitecture: " << QSysInfo::currentCpuArchitecture();
//    qInfo() << "kernelType: " << QSysInfo::kernelType();     //内核类型
//    qInfo() << "kernelVersion: " << QSysInfo::kernelVersion();    //内核版本
//    qInfo() << "machineHostName: " << QSysInfo::machineHostName();   // 主机名称
//    qInfo() << "prettyProductName: " << QSysInfo::prettyProductName();   //操作系统的产品名称
//    qInfo() << "productType: " << QSysInfo::productType();      // 系统名称
//    qInfo() << "productVersion: " << QSysInfo::productVersion();  //产品版本

    return l_SystemInfo;
}

// 获取计算主机名称
#include <QCoreApplication>
#include <QHostInfo>
#include <QDebug>
QString SysHost()
{
    qInfo() << "localHostName: " <<QHostInfo::localHostName();
    //qInfo() << "localDomainName: " <<QHostInfo::localDomainName();

    return QHostInfo::localHostName();
}

// 获取本机的IP地址
#include <QCoreApplication>
#include <QNetworkAddressEntry>
#include <QNetworkInterface>
#include <QDebug>

QString SysHostIP()
{

    QString ip="";

       QList<QNetworkInterface> interFaceList = QNetworkInterface::allInterfaces();
       for(int i=0; i< interFaceList.size(); i++)
       {
           QNetworkInterface interface = interFaceList.at(i);
           if(interface.flags().testFlag(QNetworkInterface::IsRunning))
           {
               QList<QNetworkAddressEntry> entryList = interface.addressEntries();
               foreach(QNetworkAddressEntry entry, entryList)
               {
                   if(QAbstractSocket::IPv4Protocol == entry.ip().protocol() &&
                      entry.ip() != QHostAddress::LocalHost && entry.ip().toString().startsWith("192.168."))
                   {
                       ip = entry.ip().toString();
                       break;
                   }
               }
           }
       }

    qInfo() << "localIP: " <<ip;
    return ip;
}

// 获取计算机的MAC地址
#include <QCoreApplication>
#include <QNetworkInterface>
#include <QDebug>

QString SysHostMac()
{

    QString strMac;

        QList<QNetworkInterface> netList = QNetworkInterface::allInterfaces();
        foreach(QNetworkInterface item, netList)
        {
            if((QNetworkInterface::IsUp & item.flags()) && (QNetworkInterface::IsRunning & item.flags()))
            {
                if(strMac.isEmpty() || strMac < item.hardwareAddress())
                {
                    strMac = item.hardwareAddress();
                }
            }
        }
    qInfo() << "localMac: " <<strMac;
    return strMac;

}

// 获取计算CPU相关信息
#include <QCoreApplication>
#include <QSettings>
#include <QDebug>
#include <QProcess>

QString SysHostCPU()
{
    QString m_cpuDescribe;
#ifdef WIN32
    QSettings *CPU = new QSettings("HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",QSettings::NativeFormat);
    m_cpuDescribe = CPU->value("ProcessorNameString").toString();
    delete CPU;
#else
    QProcess process;
    process.start("cat /proc/cpuinfo");					//  核心
    process.waitForStarted();
    process.waitForFinished();
    QString str = process.readAllStandardOutput();
    QVector<QString> m_msgVec;
    QStringList list = str.split("\n");
    for(int i = 0; i < list.size(); i++){
        m_msgVec << list[i];
    }
    for(int i = 0; i < m_msgVec.size(); i++){
        if(m_msgVec[i].contains("model name")){
            QStringList list = m_msgVec[i].split(":");
            m_cpuDescribe = list[1];
            break;
        }
    }
#endif
    qInfo() << "localCPU: " <<m_cpuDescribe;
    return m_cpuDescribe;

}

// 获取计算机的显卡信息
#include <QCoreApplication>
#include <QSettings>
#include <QDebug>

QString SysHostDCard()
{

    QString dcard;
    QSettings *DCard = new QSettings("HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\services\\nvlddmkm\\Device0",QSettings::NativeFormat);
    QString type = DCard->value("Device Description").toString();
    delete DCard;

    QString dType = type;
    dType.trimmed();
    if(!dType.isEmpty())
        dcard = dType;

    DCard = new QSettings("HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001\\Services\\igfx\\Device0",QSettings::NativeFormat);
    type = DCard->value("Device Description").toString();
    delete DCard;

    dType = type;
    dType.trimmed();
    if(!dType.isEmpty())
        dcard = dcard + "\n" +dType;

    DCard = new QSettings("HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001\\Services\\amdkmdap\\Device0",QSettings::NativeFormat);
    type = DCard->value("Device Description").toString();
    delete DCard;

    dType = type;
    dType.trimmed();
    if(!dType.isEmpty())
        dcard = dcard + "\n" +dType;

    dcard.trimmed();
    qInfo() << "localDCard: " <<dcard;

    return dcard;

}


// 获取计算屏幕的个数及像素情况
#include <QApplication>
#include <QDesktopWidget>
#include <QDebug>

QList<QSize> SysHostScreen()
{
    QString m_screenDescribe = "";
    QList<QSize> screenSizeList;
    QList <int> screenCountList;

    for(int i=0; i<QApplication::desktop()->screenCount(); i++)
    {
        QRect screenRect = QApplication::desktop()->screenGeometry(i);
        QSize size(screenRect.width(), screenRect.height());

        bool bExist = false;
        for(int j=0; j<screenSizeList.length(); j++)
        {
            QSize existSize = screenSizeList.at(j);
            if(size == existSize)
            {
                screenCountList[j]++;
                bExist = true;
                break;
            }
        }

        if(!bExist)
        {
            screenSizeList.append(size);
            screenCountList.append(1);
        }
    }

    QSize m_maxScreenSize = screenSizeList.at(0);
    for(int i=0; i<screenSizeList.length(); i++)
    {
        int width = screenSizeList.at(i).width();
        int height = screenSizeList.at(i).height();

        if(width > m_maxScreenSize.width() && height > m_maxScreenSize.height())
            m_maxScreenSize = screenSizeList.at(i);

        m_screenDescribe += QString ("(%1像素 x %2像素) x %3个").arg(width).arg(height).arg(screenCountList.at(i));
        if(i!= screenSizeList.length()-1)
            m_screenDescribe += "、 ";
    }

    qInfo() << "ScreenDescribe: " << m_screenDescribe;
    return screenSizeList;

}


#include <QApplication>
#include <QTcpSocket>
#include <QNetworkProxy>
#include <QDebug>

// 判断计算机是否可以联外网 SysHostNet("www.baidu.com");
bool SysHostNet(QString IP)
{
    QTcpSocket tcpClient;
    tcpClient.abort();
    tcpClient.setProxy(QNetworkProxy::NoProxy);
    tcpClient.connectToHost(IP, 80);
    //300毫秒没有连接上则判断不在线
    bool state = tcpClient.waitForConnected(300);
    if(!state){
        qInfo()<<"IP:" << IP << " connect:"<< state;
    }

    return state;

}

#include <QNetworkConfigurationManager>
#include <QDebug>
bool SysConnectNet()
{
    QNetworkConfigurationManager mgr;
    bool state = mgr.isOnline();
    qInfo()<<"connect:"<< state;
    return state;
}


  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值