8-3-Qt5开发及实列(笔记3-系统操作)

说明:此示例包含了基本的常使用的系统操作

效果如下:
在这里插入图片描述

mainwindos.cpp

#pragma execution_character_set("utf-8")
#include "mainwindow.h"

#include <QDesktopWidget>
#include <QApplication>
#include <QHostInfo>
#include <QNetworkInterface>

#include <QListView>
#include <QStringListModel>
#include <QProcess>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    QDesktopWidget *desktopWidget = QApplication::desktop();

    //1.0 cup\cupId\disk_serial
    QString cupName = getCpuName();
    QLabel *m_label_00 = new QLabel("cupName:");
    QLabel *m_label_01 = new QLabel("");
    m_label_01->setText(cupName);

    QString cupId = getCpuId();
    QLabel *m_label_10 = new QLabel("CpuId:");
    QLabel *m_label_11 = new QLabel("");
    m_label_11->setText(cupId);

    QString diskNum =getDiskNum();
    QLabel *m_label_20 = new QLabel("DiskNum:");
    QLabel *m_label_21 = new QLabel("");
    m_label_21->setText(diskNum);

    //1.1 获取屏幕分辨率
    QRect screenRect = desktopWidget->screenGeometry();
    int sWidth = screenRect.width();//屏幕宽度
    int sHeight = screenRect.height();
    QLabel *m_label00 = new QLabel("屏幕分辨率:");
    QString ip = (QString::number(sWidth,10)+"X"+QString::number(sHeight,10));
    QLabel *m_label01 = new QLabel(ip);

    //1.2 获取计算机名称
    QString localHostName = QHostInfo::localHostName();
    QLabel *m_label10 = new QLabel("本机名:");
    QLabel *m_label11 = new QLabel(localHostName);   

    //1.3 获取计算机IP
    QLabel *m_label20 = new QLabel("IP:");
    QLabel *m_label21 = new QLabel();
    QHostInfo hostInfo = QHostInfo::fromName(localHostName);
    QList<QHostAddress> listAddress = hostInfo.addresses();// 创建一个 QList,类型 QHostAddress
    if(!listAddress.isEmpty())
    {
        m_label21->setText(listAddress.first().toString());
    }

    //1.5  默认取应用程序根目录
    QString strdirpath;
    strdirpath = qApp->applicationDirPath();
    QLabel *m_label40 = new QLabel("applicationDirPath:");
    QLabel *m_label41 = new QLabel(strdirpath);

    //1.6  默认取应用程序可执行文件名称
    QString strapplicationFilePath;
    strapplicationFilePath = qApp->applicationFilePath();
    QLabel *m_label50 = new QLabel("applicationFilePath:");
    QLabel *m_label51 = new QLabel(strapplicationFilePath);

    //1.7 获取系统环境变量
    //标题
    QLabel *m_label30 = new QLabel("获取 Path 环境变量:");
    QListView *listView = new QListView();//实例 QListView
    listView->setGeometry(QRect(10,10,380,280));//QListView 位置
    QStringList strList = QProcess::systemEnvironment();//实例 QStringList 接收 path 系统变量
    QStringListModel *model = new QStringListModel(strList);//装载数据模型
    listView->setModel(model);//绑定数据



    //1.8执行系统命令
    //实例命令输入对话框
    comm = new QLineEdit();
    comm->setText("ipconfig");
    comm->setGeometry(QRect(20,20,260,25));
    //实例执行按钮
    btClick = new QPushButton(this);
    btClick->setText("执行");
    btClick->setGeometry(QRect(290,20,80,25));
    connect(btClick,SIGNAL(clicked()),this,SLOT(clickExecution()));
    //实例输出对话框
    outEdit = new QPlainTextEdit(this);
    outEdit->setGeometry(QRect(20,60,350,200));
    //实例 QProcess
    process = new QProcess;
    connect(process, SIGNAL(readyRead()), this, SLOT(readOutput()));
    //dos 命令查阅
    label = new QLabel(this);
    label->setGeometry(QRect(30,265,200,25));
    label->setText(tr("<a href=\"http://www.baidu.com/s?wd=dos 命令大全\">命令 DOS查阅</a>"));
    //开启超链接
    label->setOpenExternalLinks(true);

    QFrameshow1 = new QFrame();
    QFrameshow1->setFrameStyle(QFrame::Panel|QFrame::Sunken);//设置框架样式

    QGridLayout *childlayout0 =new QGridLayout();
    childlayout0->addWidget(m_label_00,0,0);
    childlayout0->addWidget(m_label_01,0,1);
    childlayout0->addWidget(m_label_10,1,0);
    childlayout0->addWidget(m_label_11,1,1);
    childlayout0->addWidget(m_label_20,2,0);
    childlayout0->addWidget(m_label_21,2,1);

    QGridLayout *childlayout1 = new QGridLayout();
    childlayout1->addWidget(m_label00,0,0);
    childlayout1->addWidget(m_label01,0,1);
    childlayout1->addWidget(m_label10,1,0);
    childlayout1->addWidget(m_label11,1,1);
    childlayout1->addWidget(m_label20,2,0);
    childlayout1->addWidget(m_label21,2,1);
    childlayout1->addWidget(m_label40,3,0);
    childlayout1->addWidget(m_label41,3,1);
    childlayout1->addWidget(m_label50,4,0);
    childlayout1->addWidget(m_label51,4,1);

    QVBoxLayout *childlayout2 = new QVBoxLayout;
    childlayout2->addWidget(m_label30);
    childlayout2->addWidget(listView);

    QGridLayout *childlayout3 = new QGridLayout();
    childlayout3->addWidget(comm);
    childlayout3->addWidget(btClick);
    childlayout3->addWidget(outEdit);
    //childlayout3->addWidget(process);
    childlayout3->addWidget(label);

    QVBoxLayout *mainlayout = new QVBoxLayout(QFrameshow1);
    mainlayout->addLayout(childlayout0);
    mainlayout->addLayout(childlayout1);
    mainlayout->addLayout(childlayout2);
    mainlayout->addLayout(childlayout3);
    //显示布局控件
    QWidget *m_widget = new QWidget();
    m_widget->setLayout(mainlayout);
    this->setCentralWidget(m_widget);
}

MainWindow::~MainWindow()
{

}

//执行 DOS 命令
void MainWindow::clickExecution()
{
 //定义变量接收 dos 命令
 QString info = comm->text();
 //执行命令
 process->start(info);
 //绑定反馈值
 outEdit->setPlainText(output);
}
//QProcess 事件
void MainWindow::readOutput()
{
 //接收反馈信息
 output +=process->readAll();
 //将返回值绑定控件

 outEdit->setPlainText(output);
}


QString MainWindow::getWMIC(const QString &cmd)
{
    //获取cpu名称:wmic cpu get Name
    //获取cpu核心数:wmic cpu get NumberOfCores
    //获取cpu线程数:wmic cpu get NumberOfLogicalProcessors
    //查询cpu序列号:wmic cpu get processorid
    //查询主板序列号:wmic baseboard get serialnumber
    //查询BIOS序列号:wmic bios get serialnumber
    //查看硬盘:wmic diskdrive get serialnumber
    QProcess p;
    p.start(cmd);
    p.waitForFinished();
    QString result = QString::fromLocal8Bit(p.readAllStandardOutput());
    qDebug()<<result<<endl;
    QStringList list = cmd.split(" ");
    result = result.remove(list.last(), Qt::CaseInsensitive);
    result = result.replace("\r", "");//换行
    result = result.replace("\n", "");//光标到行首
    result = result.simplified();//simplified 去掉开头与结尾的空格,中间空格序列替换为单空格
    return result;
}
QString MainWindow::getCpuName(){
    return getWMIC("wmic cpu get name");
}
QString MainWindow::getCpuId(){
    return getWMIC("wmic cpu get processorid");
}
QString MainWindow::getDiskNum(){
    return getWMIC("wmic diskdrive where index=0 get serialnumber");
}


mainwindow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QFrame>
#include <QLabel>
#include <QWidget>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QPlainTextEdit>
#include <QProcess>
#include <QLabel>
class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
    QFrame *QFrameshow1;

private:
    QLineEdit *comm; //dos 命令输入框
    QPlainTextEdit *outEdit; //命令执行反馈框
    QPushButton *btClick; //执行按钮
    QProcess *process; //QProcess
    QString output; //接收反馈信息
    QLabel *label;

    QString getCpuName();
    QString getCpuId();
    QString getDiskNum();
    QString getXorEncryptDecrypt(const QString &data, char key);
    QString getWMIC(const QString &cmd);
private slots:
    void clickExecution(); //点击按钮事件
    void readOutput(); //QProcess 事件

};

#endif // MAINWINDOW_H

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值