码迷,mamicode.com
首页 > 系统相关 > 详细

linux系统Qt实现简单的任务管理器

时间:2018-06-29 00:04:21      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:页面   平台   开发   mem   upd   line   pfile   优先级   任务   

 

 

继续上次的操作系统课设,这次需要设计一个简单的任务管理器,大部分人选择GTK来实现,我剑走偏锋,使用Qt来完成这个任务。

用户和应用程序可以通过/proc得到系统的信息,并可以改变内核的某些参数。由于系统的信息是动态改变的,所以用户或应用程序读取proc文件时,proc文件系统是动态从系统内核读出所需信息并提交的。

我们要显示系统信息,只需进行相应的文件操作就行了。

首先需要下载一份Qt的SDK,这是Qt的英文官网:http://qt.nokia.com/,当然也有中文版的:http://qt.nokia.com/title-cn/。

别问我为什么有个nokia,那是因为Qt是诺基亚开发的一个跨平台的C++图形用户界面应用程序框架。

Qt商业版只能试用30天,不过有GPL版的,可以免费使用。官网上还有一个非常不错的免费Qt集成开发环境Qt Creator IDE。我使用的就是这个软件:

打开相应的文件,读取所需要的信息,将其显示在控件上就可以了。

我采用的是Qt来实现图形界面。

 技术分享图片

技术分享图片

技术分享图片

 

工程文件夹:

 技术分享图片 

编译完成后的实现效果:

 技术分享图片

技术分享图片

技术分享图片

技术分享图片

技术分享图片

 

这个实验总的来讲还是比较简单的,源码如下:

 

main.cpp

 

  1. #include <QtGui/QApplication> 
  2. #include "mainwindow.h" 
  3.  
  4. int main(int argc, char *argv[]) 
  5. { 
  6.     QApplication a(argc, argv); //应用程序类,每个应用程序有且只有一个 
  7.     MainWindow w; //实例化MainWindow类 
  8.     w.show(); //显示界面 
  9.     return a.exec(); //进入应用程序的循环中,直到程序退出 
  10. } 

 

 

mainwindow.h

  1. #ifndef MAINWINDOW_H 
  2. #define MAINWINDOW_H 
  3.  
  4. #include <QMainWindow> 
  5.  
  6. namespace Ui { 
  7.     class MainWindow; 
  8. } 
  9.  
  10. class MainWindow : public QMainWindow 
  11. { 
  12.     Q_OBJECT 
  13. public: 
  14.     explicit MainWindow(QWidget *parent = 0); 
  15.     ~MainWindow(); 
  16. private: 
  17.     Ui::MainWindow *ui; //界面资源类,所有的界面元素都是通过该类来调用 
  18.     QTimer *timer; //计时器 
  19.  
  20. private slots: 
  21.     void on_pushButton_pkill_clicked(); 
  22.     void on_pushButton_prefresh_clicked(); 
  23.     void on_pushButton_Model_install_clicked(); 
  24.     void on_pushButton_Model_remove_clicked(); 
  25.     void on_pushButton_Model_refresh_clicked(); 
  26.     void on_pushButton_reboot_clicked(); 
  27.     void on_pushButton_halt_clicked(); 
  28.     void on_tabWidget_INFO_currentChanged(int index); 
  29.     void timer_update_currentTabInfo(); 
  30.     //显示tab中的内容 
  31.     void show_tabWidgetInfo(int index); 
  32. }; 
  33.  
  34. #endif // MAINWINDOW_H 

 

mainwindow.cpp

 

  1. #include "mainwindow.h" 
  2. #include "ui_mainwindow.h" 
  3.  
  4. #include <QFile> 
  5. #include <QMessageBox> 
  6. #include <QDir> 
  7. #include <QListWidget> 
  8. #include <QListWidgetItem> 
  9. #include <QStringList> 
  10. #include <QTimer> 
  11.  
  12. int a0 = 0, a1 = 0, b0 = 0, b1 = 0; 
  13.  
  14. MainWindow::MainWindow(QWidget *parent) : //构造函数,初始化ui,计时器 
  15.     QMainWindow(parent), 
  16.     ui(new Ui::MainWindow) 
  17. { 
  18.     ui->setupUi(this); 
  19.     timer = new QTimer(this); 
  20.     QWidget::connect( timer, SIGNAL( timeout() ), this, SLOT( timer_update_currentTabInfo() ) ); 
  21.     QWidget::connect( ui->tabWidget_INFO, SIGNAL( currentChanged() ), 
  22.                       this, SLOT( on_tabWidget_currentChanged() ) ); 
  23.     timer->start(1000); 
  24. } 
  25.  
  26. MainWindow::~MainWindow() 
  27. { 
  28.     delete ui; 
  29.     delete timer; 
  30. } 
  31.  
  32. void MainWindow::timer_update_currentTabInfo() 
  33. { 
  34.     int index = ui->tabWidget_INFO->currentIndex(); 
  35.     //定时器只刷新内存tab页面,用于进度条动态显示 
  36.     if (index == 0) 
  37.     { 
  38.         show_tabWidgetInfo(index); 
  39.     } 
  40. } 
  41.  
  42. void MainWindow::show_tabWidgetInfo(int index) 
  43. { 
  44.     QString tempStr; //读取文件信息字符串 
  45.     QFile tempFile; //用于打开系统文件 
  46.     int pos; //读取文件的位置 
  47.  
  48.     if (index == 0) //内存資源 
  49.     { 
  50.         tempFile.setFileName("/proc/meminfo"); //打开内存信息文件 
  51.         if ( !tempFile.open(QIODevice::ReadOnly) ) 
  52.         { 
  53.             QMessageBox::warning(this, tr("warning"), tr("The meminfo file can not open!"), QMessageBox::Yes); 
  54.             return ; 
  55.         } 
  56.         QString memTotal; 
  57.         QString memFree; 
  58.         QString memUsed; 
  59.         QString swapTotal; 
  60.         QString swapFree; 
  61.         QString swapUsed; 
  62.         int nMemTotal, nMemFree, nMemUsed, nSwapTotal, nSwapFree, nSwapUsed; 
  63.  
  64.         while (1) 
  65.         { 
  66.             tempStr = tempFile.readLine(); 
  67.             pos = tempStr.indexOf("MemTotal"); 
  68.             if (pos != -1) 
  69.             { 
  70.                 memTotal = tempStr.mid(pos+10, tempStr.length()-13); 
  71.                 memTotal = memTotal.trimmed(); 
  72.                 nMemTotal = memTotal.toInt()/1024; 
  73.             } 
  74.             else if (pos = tempStr.indexOf("MemFree"), pos != -1) 
  75.             { 
  76.                 memFree = tempStr.mid(pos+9, tempStr.length()-12); 
  77.                 memFree = memFree.trimmed(); 
  78.                 nMemFree = memFree.toInt()/1024; 
  79.             } 
  80.             else if (pos = tempStr.indexOf("SwapTotal"), pos != -1) 
  81.             { 
  82.                 swapTotal = tempStr.mid(pos+11, tempStr.length()-14); 
  83.                 swapTotal = swapTotal.trimmed(); 
  84.                 nSwapTotal = swapTotal.toInt()/1024; 
  85.             } 
  86.             else if (pos = tempStr.indexOf("SwapFree"), pos != -1) 
  87.             { 
  88.                 swapFree = tempStr.mid(pos+10,tempStr.length()-13); 
  89.                 swapFree = swapFree.trimmed(); 
  90.                 nSwapFree = swapFree.toInt()/1024; 
  91.                 break; 
  92.             } 
  93.         } 
  94.  
  95.         nMemUsed = nMemTotal - nMemFree; 
  96.         nSwapUsed = nSwapTotal - nSwapFree; 
  97.  
  98.         memUsed = QString::number(nMemUsed, 10); 
  99.         swapUsed = QString::number(nSwapUsed, 10); 
  100.         memFree = QString::number(nMemFree, 10); 
  101.         memTotal = QString::number(nMemTotal, 10); 
  102.         swapFree = QString::number(nSwapFree, 10); 
  103.         swapTotal = QString::number(nSwapTotal, 10); 
  104.  
  105.         ui->label_RAM_Used->setText(memUsed+" MB"); 
  106.         ui->label_RAM_Left->setText(memFree+" MB"); 
  107.         ui->label_RAM_Total->setText(memTotal+" MB"); 
  108.         ui->label_SWAP_Used->setText(swapUsed+" MB"); 
  109.         ui->label_SWAP_Left->setText(swapFree+" MB"); 
  110.         ui->label_SWAP_Total->setText(swapTotal+" MB"); 
  111.  
  112.         ui->progressBar_RAM->setValue(nMemUsed*100/nMemTotal); 
  113.         ui->progressBar_SWAP->setValue(nSwapUsed*100/nSwapTotal); 
  114.  
  115.         tempFile.close(); //关闭内存信息文件 
  116.  
  117.         int tt = 2; //取2个点采样计算cpu当前利用律 
  118.         int cpuInfo[2][7]; 
  119.         int cpuTotal[2][2]; 
  120.         while (tt) 
  121.         { 
  122.             tempFile.setFileName("/proc/stat"); //打开CPU使用状态信息 
  123.             if ( !tempFile.open(QIODevice::ReadOnly) ) 
  124.             { 
  125.                 QMessageBox::warning(this, tr("warning"), tr("The stat file can not open!"), QMessageBox::Yes); 
  126.                 return; 
  127.             } 
  128.             tempStr = tempFile.readLine(); 
  129.             for (int i = 0; i < 7; i++) 
  130.             { 
  131.                 cpuInfo[2-tt][i] = tempStr.section(" ", i+1, i+1).toInt(); 
  132.                 cpuTotal[1][2-tt] += cpuInfo[2-tt][i]; 
  133.                 if (i == 3) 
  134.                 { 
  135.                     cpuTotal[0][2-tt] += cpuInfo[2-tt][i]; 
  136.                 } 
  137.             } 
  138.             tt--; 
  139.             tempFile.close(); //关闭stat文件 
  140.         } 
  141.  
  142.         int a = cpuTotal[0][1] - cpuTotal[0][0]; 
  143.         int b = cpuTotal[1][1] - cpuTotal[1][0]; 
  144.         if (a < 0) 
  145.         { 
  146.             a = -a; 
  147.         } 
  148.         if (b < 0) 
  149.         { 
  150.             b = -b; 
  151.         } 
  152.         ui->progressBar_CPU->setValue(a*100/b); 
  153.  
  154.         tempFile.setFileName("/proc/stat"); 
  155.         if ( !tempFile.open(QIODevice::ReadOnly) ) 
  156.         { 
  157.             QMessageBox::warning(this, tr("warning"), tr("The stat file can not open!"), QMessageBox::Yes); 
  158.             return; 
  159.         } 
  160.  
  161.         tempStr = tempFile.readLine(); 
  162.         a0 = a1; 
  163.         b0 = b1; 
  164.         a1 = b1 = 0; 
  165.         int gg; 
  166.         for (int i = 0; i < 7; i++) 
  167.         { 
  168.             b1 += tempStr.section(" ", i+2, i+2).toInt(); 
  169.             gg = b1; 
  170.             if (i == 3) 
  171.             { 
  172.                 a1 += tempStr.section(" ", i+2, i+2).toInt(); 
  173.             } 
  174.         } 
  175.         int m, n; 
  176.         m = a1 - a0; 
  177.         n = b1 - b0; 
  178.         if (m < 0) 
  179.         { 
  180.             m = -m; 
  181.         } 
  182.         if (n < 0) 
  183.         { 
  184.             n = -n; 
  185.         } 
  186.         ui->progressBar_CPU->setValue( (n-m)*100/n ); 
  187.         tempFile.close(); //关闭stat文件 
  188.     } 
  189.  
  190.     else if (index == 1) //进程信息 
  191.     { 
  192.         ui->listWidget_process->clear(); 
  193.         QDir qd("/proc"); 
  194.         QStringList qsList = qd.entryList(); 
  195.         QString qs = qsList.join("\n"); 
  196.         QString id_of_pro; 
  197.         bool ok; 
  198.         int find_start = 3; 
  199.         int a, b; 
  200.         int nProPid; //进程PID 
  201.         int number_of_sleep = 0, number_of_run = 0, number_of_zombie = 0; 
  202.         int totalProNum = 0; //进程总数 
  203.         QString proName; //进程名 
  204.         QString proState; //进程状态 
  205.         QString proPri; //进程优先级 
  206.         QString proMem; //进程占用内存 
  207.         QListWidgetItem *title = new QListWidgetItem("PID\t" + QString::fromUtf8("名称") + "\t\t" + 
  208.                                                      QString::fromUtf8("状态") + "\t" + 
  209.                                                      QString::fromUtf8("优先级") + "\t" + 
  210.                                                      QString::fromUtf8("占用内存"), ui->listWidget_process); 
  211.         //循环读取进程 
  212.         while (1) 
  213.         { 
  214.             //获取进程PID 
  215.             a = qs.indexOf("\n", find_start); 
  216.             b = qs.indexOf("\n", a+1); 
  217.             find_start = b; 
  218.             id_of_pro = qs.mid(a+1, b-a-1); 
  219.             totalProNum++; 
  220.             nProPid = id_of_pro.toInt(&ok, 10); 
  221.             if(!ok) 
  222.             { 
  223.                 break; 
  224.             } 
  225.  
  226.             //打开PID所对应的进程状态文件 
  227.             tempFile.setFileName("/proc/" + id_of_pro + "/stat"); 
  228.             if ( !tempFile.open(QIODevice::ReadOnly) ) 
  229.             { 
  230.                 QMessageBox::warning(this, tr("warning"), tr("The pid stat file can not open!"), QMessageBox::Yes); 
  231.                 return; 
  232.             } 
  233.             tempStr = tempFile.readLine(); 
  234.             if (tempStr.length() == 0) 
  235.             { 
  236.                 break; 
  237.             } 
  238.             a = tempStr.indexOf("("); 
  239.             b = tempStr.indexOf(")"); 
  240.             proName = tempStr.mid(a+1, b-a-1); 
  241.             proName.trimmed(); //删除两端的空格 
  242.             proState = tempStr.section(" ", 2, 2); 
  243.             proPri = tempStr.section(" ", 17, 17); 
  244.             proMem = tempStr.section(" ", 22, 22); 
  245.  
  246.             switch ( proState.at(0).toLatin1() ) 
  247.             { 
  248.                 case ‘S‘:   number_of_sleep++; break; //Sleep 
  249.                 case ‘R‘:   number_of_run++; break; //Running 
  250.                 case ‘Z‘:   number_of_zombie++; break; //Zombie 
  251.                 default :   break; 
  252.             } 
  253.  
  254.             if (proName.length() >= 12) 
  255.             { 
  256.                 QListWidgetItem *item = new QListWidgetItem(id_of_pro + "\t" + 
  257.                                                             proName + "\t" + 
  258.                                                             proState + "\t" + 
  259.                                                             proPri + "\t" + 
  260.                                                             proMem, ui->listWidget_process); 
  261.             } 
  262.             else 
  263.             { 
  264.                 QListWidgetItem *item = new QListWidgetItem(id_of_pro + "\t" + 
  265.                                                             proName + "\t\t" + 
  266.                                                             proState + "\t" + 
  267.                                                             proPri + "\t" + 
  268.                                                             proMem, ui->listWidget_process); 
  269.             } 
  270.         } 
  271.         QString temp; 
  272.         temp = QString::number(totalProNum, 10); 
  273.         ui->label_pNum->setText(temp); 
  274.         temp = QString::number(number_of_run, 10); 
  275.         ui->label_pRun->setText(temp); 
  276.         temp = QString::number(number_of_sleep, 10); 
  277.         ui->label_pSleep->setText(temp); 
  278.         temp = QString::number(number_of_zombie, 10); 
  279.         ui->label_pZombie->setText(temp); 
  280.  
  281.         tempFile.close(); //关闭该PID进程的状态文件 
  282.     } 
  283.  
  284.     else if (index == 2) //模块信息 
  285.     { 
  286.         ui->listWidget_model->clear(); 
  287.         tempFile.setFileName("/proc/modules"); //打开模块信息文件 
  288.         if ( !tempFile.open(QIODevice::ReadOnly) ) 
  289.         { 
  290.             QMessageBox::warning(this, tr("warning"), tr("The modules file can not open!"), QMessageBox::Yes); 
  291.             return ; 
  292.         } 
  293.         //设置模块首行项目 
  294.         QListWidgetItem *title = new QListWidgetItem( QString::fromUtf8("名称") + "\t\t\t" + 
  295.                                                       QString::fromUtf8("使用内存数") + "\t\t" + 
  296.                                                       QString::fromUtf8("使用次數"), ui->listWidget_model); 
  297.  
  298.         QString mod_Name, mod_Mem, mod_Num; 
  299.         //循环读取文件内容,查找需要的信息 
  300.         while (1) 
  301.         { 
  302.             tempStr = tempFile.readLine(); 
  303.             if (tempStr.length() == 0) 
  304.             { 
  305.                 break; 
  306.             } 
  307.             mod_Name = tempStr.section(" ", 0, 0); 
  308.             mod_Mem = tempStr.section(" ", 1, 1); 
  309.             mod_Num = tempStr.section(" ", 2, 2); 
  310.             if (mod_Name.length() > 10) 
  311.             { 
  312.                 QListWidgetItem *item = new QListWidgetItem(mod_Name + "\t\t" + 
  313.                                                             mod_Mem + "\t\t" + 
  314.                                                             mod_Num, ui->listWidget_model); 
  315.             } 
  316.             else 
  317.             { 
  318.                 QListWidgetItem *item = new QListWidgetItem(mod_Name + "\t\t\t" + 
  319.                                                             mod_Mem + "\t\t" + 
  320.                                                             mod_Num, ui->listWidget_model); 
  321.             } 
  322.         } 
  323.         tempFile.close(); //关闭模块信息文件 
  324.     } 
  325.  
  326.     else if (index == 3) //系统信息 
  327.     { 
  328.         //int ok; 
  329.         tempFile.setFileName("/proc/cpuinfo"); //打开CPU信息文件 
  330.         if ( !tempFile.open(QIODevice::ReadOnly) ) 
  331.         { 
  332.             QMessageBox::warning(this, tr("warning"), tr("The cpuinfo file can not open!"), QMessageBox::Yes); 
  333.             return; 
  334.         } 
  335.  
  336.         //循环读取文件内容,查找需要的信息 
  337.         while (1) 
  338.         { 
  339.             tempStr = tempFile.readLine(); 
  340.             pos = tempStr.indexOf("model name"); 
  341.             if (pos != -1) 
  342.             { 
  343.                 pos += 13; //跳过前面的"model name:"所占用的字符 
  344.                 QString *cpu_name = new QString( tempStr.mid(pos, tempStr.length()-13) ); 
  345.                 ui->label_CPUName->setText(*cpu_name); 
  346.             } 
  347.             else if (pos = tempStr.indexOf("vendor_id"), pos != -1) 
  348.             { 
  349.                 pos += 12; //跳过前面的"vendor_id:"所占用的字符 
  350.                 QString *cpu_type = new QString( tempStr.mid(pos, tempStr.length()-12) ); 
  351.                 ui->label_CPUType->setText(*cpu_type); 
  352.             } 
  353.             else if (pos = tempStr.indexOf("cpu MHz"), pos != -1) 
  354.             { 
  355.                 pos += 11; //跳过前面的"cpu MHz:"所占用的字符 
  356.                 QString *cpu_frq = new QString( tempStr.mid(pos, tempStr.length()-11) ); 
  357.                 double cpufrq = cpu_frq->toDouble(); //4核CPU 
  358.                 cpu_frq->setNum(cpufrq*4); 
  359.                 ui->label_CPUFrequency->setText(*cpu_frq + " HZ"); 
  360.             } 
  361.             else if (pos = tempStr.indexOf("cache size"), pos!=-1) 
  362.             { 
  363.                 pos += 13; //跳过前面的"cache size:"所占用的字符 
  364.                 QString *cache_size = new QString( tempStr.mid(pos, tempStr.length()-16) ); 
  365.                 int cachesize = cache_size->toInt(); //4核CPU 
  366.                 cache_size->setNum(cachesize*4); 
  367.                 ui->label_CatheCapacity->setText(*cache_size + " KB"); 
  368.             } 
  369.             else //跳过其他的内容 
  370.             { 
  371.             } 
  372.         } 
  373.         tempFile.close(); //关闭CPU信息文件 
  374.  
  375.         //打开操作系统信息文件 
  376.         tempFile.setFileName("/proc/version"); 
  377.         if ( !tempFile.open(QIODevice::ReadOnly) ) 
  378.         { 
  379.             QMessageBox::warning(this, tr("warning"), tr("The version file can not open!"), QMessageBox::Yes); 
  380.             return ; 
  381.         } 
  382.         tempStr = tempFile.readLine(); 
  383.         pos = tempStr.indexOf("version"); 
  384.         QString *os_version = new QString( tempStr.mid(0, pos-1) ); 
  385.         ui->label_SystemType->setText(*os_version); 
  386.  
  387.         int pos1 = tempStr.indexOf("("); 
  388.         QString *os_type = new QString( tempStr.mid(pos, pos1-pos-1) ); 
  389.         ui->label_SystemVersion->setText(*os_type); 
  390.  
  391.         pos = tempStr.indexOf("gcc version"); 
  392.         pos1 = tempStr.indexOf("#"); 
  393.         QString *gcc_info = new QString( tempStr.mid(pos+12, pos1-pos-14) ); 
  394.         ui->label_GCCVersion->setText(*gcc_info); 
  395.  
  396.         tempFile.close(); //关闭操作系统信息文件 
  397.     } 
  398.  
  399.     else //说明 
  400.     { 
  401.     } 
  402.     return; 
  403. } 
  404.  
  405. void MainWindow::on_pushButton_halt_clicked() 
  406. { 
  407.     system("halt"); 
  408. } 
  409.  
  410. void MainWindow::on_pushButton_reboot_clicked() 
  411. { 
  412.     system("reboot"); 
  413. } 
  414.  
  415.  
  416. void MainWindow::on_tabWidget_INFO_currentChanged(int index) 
  417. { 
  418.     show_tabWidgetInfo(index); //显示tab中的内容 
  419.     return ; 
  420. } 
  421.  
  422. void MainWindow::on_pushButton_pkill_clicked() 
  423. { 
  424.     //获得进程号 
  425.     QListWidgetItem *item = ui->listWidget_process->currentItem(); 
  426.     QString pro = item->text(); 
  427.     pro = pro.section("\t", 0, 0); 
  428.     system("kill " + pro.toLatin1()); 
  429.     QMessageBox::warning(this, tr("kill"), QString::fromUtf8("该进程已被杀死!"), QMessageBox::Yes); 
  430.     //回到进程信息tab表 
  431.     show_tabWidgetInfo(1); 
  432. } 
  433.  
  434. void MainWindow::on_pushButton_prefresh_clicked() 
  435. { 
  436.     show_tabWidgetInfo(1); 
  437. } 
  438.  
  439. void MainWindow::on_pushButton_Model_install_clicked() 
  440. { 
  441.     show_tabWidgetInfo(2); //安装模块还不知道如何实现 
  442. } 
  443.  
  444. void MainWindow::on_pushButton_Model_remove_clicked() 
  445. { 
  446.     show_tabWidgetInfo(2); //卸载模块还不知道如何实现 
  447. } 
  448.  
  449. void MainWindow::on_pushButton_Model_refresh_clicked() 
  450. { 
  451.     show_tabWidgetInfo(2); 
  452. } 

 

转自http://blog.51cto.com/rangercyh/521262

linux系统Qt实现简单的任务管理器

标签:页面   平台   开发   mem   upd   line   pfile   优先级   任务   

原文地址:https://www.cnblogs.com/liushui-sky/p/9241158.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!