使用C++中Qt框架获取当前计算机CPU型号(Windows与Linux通用)

原理:

Windows:使用了wmic服务,也就是这条命令wmic cpu get name

Linux:读取了/proc/cpuinfo,文件,从而读取CPU信息,

 

源码下载链接如下:

https://download.csdn.net/download/qq78442761/10803173

 

下面是效果!

在Windows机器上进行调用:

使用C++中Qt框架获取当前计算机CPU型号(Windows与Linux通用)_第1张图片

在Linux上调用如下图:

使用C++中Qt框架获取当前计算机CPU型号(Windows与Linux通用)_第2张图片

 

源码如下:

cpuinfo.h

#ifndef CPUINFO
#define CPUINFO

#include 
#include 
#include 
#include "linuxfactory.h"

class CPUInfo{

public:
    static QString getCommand(){

        QString cmd;
#ifdef Q_OS_WIN
        cmd = "wmic cpu get name";
#else
        cmd = "cat /proc/cpuinfo";
#endif
        return cmd;
    }

    static QString getCPUInformation(const QVector strVec){

        QString cpuInfo = "NULL";

#ifdef Q_OS_WIN
        for(int i = 0;i < strVec.size(); i++){
            if(strVec[i].contains("CPU")){
                QString msg = strVec[i];
                cpuInfo =msg.remove("\r");
            }
        }
#else

        LinuxOperation *linuxOpera = NULL;

        try{
            linuxOpera = LinuxOperationFactory::createOperate("model name");

        }
        catch(QString err){

            QMessageBox::information(NULL, "info", err);
        }

        if(linuxOpera != NULL){

            cpuInfo = linuxOpera->getInfomation(strVec);
            delete linuxOpera;
        }

#endif

        return cpuInfo;
    }
};

#endif // CPUINFO

linuxfactory.h

#ifndef LINUXFACTORY
#define LINUXFACTORY

#include "linuxOperation.h"

class LinuxOperationFactory{

public:
    static LinuxOperation *createOperate(const QString operateStr){

        LinuxOperation *linuxOperation;

        if(operateStr == "model name")
            linuxOperation = new OperationModelName;
        else
            throw QString("operateStr is error!");

        return linuxOperation;
    }
};


#endif // LINUXFACTORY

linuxOperation.h

#ifndef LINUXOPERATION_H
#define LINUXOPERATION_H

#include 
#include 

class LinuxOperation
{
public:
    virtual QString getInfomation(const QVector strVec) = 0;
    virtual ~LinuxOperation();
};


class OperationModelName: public LinuxOperation{

public:
    QString getInfomation(const QVector strVec);
    ~OperationModelName();
};

#endif // LINUXOPERATION_H

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 

QT_BEGIN_NAMESPACE
class QProcess;
QT_END_NAMESPACE

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

protected:
    void catShellCalled();

protected slots:
    void readProcess();
    void processFinished();

private:
    Ui::Widget *ui;
    QProcess *m_catProcess;
    QVector m_msgVec;
};

#endif // WIDGET_H

linuxOperation.cpp

#include "linuxOperation.h"
#include 

LinuxOperation::~LinuxOperation()
{
    qDebug()<< "~LinuxOperation() called!";
}

QString OperationModelName::getInfomation(const QVector strVec){

    for(int i = 0; i < strVec.size(); i++){
        if(strVec[i].contains("model name")){
            //qDebug()<< strVec[i];
            QStringList list = strVec[i].split(":");
            return list[1];
        }
    }

    throw QString("can't find the model name");
}

OperationModelName::~OperationModelName(){

    qDebug()<< "~OperationModelName() called!";
}

main.cpp

#include "widget.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

wiget.cpp

#include "widget.h"
#include "ui_widget.h"
#include "cpuinfo.h"
#include 
#include 

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    this->setWindowTitle("CSDN IT1995");

    m_catProcess = new QProcess;
    connect(m_catProcess, SIGNAL(readyRead()), this, SLOT(readProcess()));
    connect(m_catProcess, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(processFinished()));
    catShellCalled();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::catShellCalled()
{
    QString cmdCat = CPUInfo::getCommand();
    m_catProcess->start(cmdCat);
}

void Widget::readProcess()
{
    QString allMsg = m_catProcess->readAll();
    QStringList list = allMsg.split("\n");
    for(int i = 0; i < list.size(); i++){
        m_msgVec << list[i];
    }
}

void Widget::processFinished()
{
    qDebug()<< "processFinished() called";
    QString cpuInfo = CPUInfo::getCPUInformation(m_msgVec);
    ui->label->setText(cpuInfo);
}

 

你可能感兴趣的:(C/C++,Qt,Linux,HackerCode)