Qt笔记-获取Windows下目前运行的进程信息

目录

 

 

基本概念

代码与实例

源码下载


 

 

基本概念

知识点如下:

CreateToolhelp32Snapshot    获取当前系统进程快照

void Thread::getSnapshot()
{
    HANDLE hProcess = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
    if(!hProcess){

        qDebug() << "CreateToolhelp32Snapshot failed!";
        return;
    }

    PROCESSENTRY32 info;
    info.dwSize = sizeof(PROCESSENTRY32);
    if(!Process32First(hProcess, &info)){

        qDebug() << "Process32First null";
        return;
    }

    bool haveNext = true;
    while(haveNext){

        haveNext = Process32Next(hProcess, &info);
        if(haveNext){

            HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, TRUE, info.th32ProcessID);

            insertInfo(QString::number(info.th32ProcessID), QString::number(info.cntUsage),
                       QString::number(info.th32ParentProcessID), QString::number(info.pcPriClassBase),
                       QString::number((DWORD)hProcess), QString::fromWCharArray(info.szExeFile));

//            qDebug() << info.th32ProcessID << "  " << info.pcPriClassBase << "  " << QString::fromWCharArray(info.szExeFile) << "  " << hProcess;
        }
    }
//    qDebug() << "Over!";
}

通过此方式获取当前系统的进程信息

 

另外一个是关于QTableWidget相关设置:

    QStringList headList;
    headList << "进程ID" << "     进程名     " << "父类进程ID" << "进程句柄" << "优先级";
    ui->tableWidget->setColumnCount(5);
    ui->tableWidget->setHorizontalHeaderLabels(headList);
//    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget->setColumnWidth(1, 150);
    ui->tableWidget->horizontalHeader()->setStretchLastSection(true);


    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableWidget->setShowGrid(false);

设置表头数据,以及最后一列填充,以及不可编辑,以及设置每一项Stretch

 

 

代码与实例

程序运行截图如下:

Qt笔记-获取Windows下目前运行的进程信息_第1张图片

上面那有一部分关键代码。

下面给出补充的关键代码:

#include "widget.h"
#include "ui_widget.h"
#include "thread.h"
#include 
#include 

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

    QStringList headList;
    headList << "进程ID" << "     进程名     " << "父类进程ID" << "进程句柄" << "优先级";
    ui->tableWidget->setColumnCount(5);
    ui->tableWidget->setHorizontalHeaderLabels(headList);
//    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget->setColumnWidth(1, 150);
    ui->tableWidget->horizontalHeader()->setStretchLastSection(true);


    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableWidget->setShowGrid(false);


    startTimer(1000);
}

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

void Widget::addNew()
{
    QVector vec = m_thread->getVec();


    for(ProcessInfo info : vec){

        bool have = false;
        for(int i = 0; i < ui->tableWidget->rowCount(); i++){

            if(ui->tableWidget->item(i, 0)->text() == info.processID){

               have = true;
            }
        }
        if(!have){

            ui->tableWidget->insertRow(0);
            ui->tableWidget->setItem(0, 0, new QTableWidgetItem(info.processID));
            ui->tableWidget->setItem(0, 1, new QTableWidgetItem(info.processName));
            ui->tableWidget->setItem(0, 2, new QTableWidgetItem(info.parentProcessID));
            ui->tableWidget->setItem(0, 3, new QTableWidgetItem(info.processHandle));
            ui->tableWidget->setItem(0, 4, new QTableWidgetItem(info.processPri));
        }
    }
}

void Widget::removeOld()
{
    QVector vec = m_thread->getVec();


    for(int i = 0; i < ui->tableWidget->rowCount(); i++){

        bool have = false;
        for(ProcessInfo info : vec){

            if(info.processID == ui->tableWidget->item(i, 0)->text()){

                have = true;
                continue;
            }
        }

        if(!have){

            ui->tableWidget->removeRow(i);
        }
    }
}

void Widget::timerEvent(QTimerEvent *event)
{
    Q_UNUSED(event)
    m_thread->clearVec();
    m_thread->getSnapshot();
    addNew();
    removeOld();
}

 

 

 

源码下载

打包下载地址:https://github.com/fengfanchen/Qt/tree/master/ProcessList

 

 

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