歡迎您光臨本站 註冊首頁

linux系統Qt實現簡單的任務管理器

←手機掃碼閱讀     火星人 @ 2014-03-09 , reply:0

繼續上次的操作系統課設,這次需要設計一個簡單的任務管理器,大部分人選擇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. int main(int

    argc, char *argv[])
  4. {

  5. QApplication a(argc, argv); //應用程序類,每個應用程序有且只有一個
  6. MainWindow w; //實例化MainWindow類
  7. w.show();

    //顯示界面
  8. return

    a.exec(); //進入應用程序的循環中,直到程序退出
  9. }

mainwindow.h

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3. #include <QMainWindow>
  4. namespace Ui {
  5. class MainWindow;
  6. }
  7. class MainWindow : public QMainWindow
  8. {
  9. Q_OBJECT
  10. public:
  11. explicit MainWindow(QWidget *parent = 0);
  12. ~MainWindow();
  13. private:
  14. Ui::MainWindow *ui; //界面資源類,所有的界面元素都是通過該類來調用
  15. QTimer *timer; //計時器
  16. private slots:
  17. void on_pushButton_pkill_clicked();
  18. void on_pushButton_prefresh_clicked();
  19. void on_pushButton_Model_install_clicked();
  20. void on_pushButton_Model_remove_clicked();
  21. void on_pushButton_Model_refresh_clicked();
  22. void on_pushButton_reboot_clicked();
  23. void on_pushButton_halt_clicked();
  24. void on_tabWidget_INFO_currentChanged(int index);
  25. void timer_update_currentTabInfo();
  26. //顯示tab中的內容
  27. void show_tabWidgetInfo(int index);
  28. };
  29. #endif // MAINWINDOW_H

mainwindow.cpp

  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QFile>

  4. #include <QMessageBox>
  5. #include <QDir>
  6. #include <QListWidget>
  7. #include <QListWidgetItem>
  8. #include <QStringList>
  9. #include <QTimer>
  10. int a0 = 0, a1 = 0, b0 = 0, b1 = 0;
  11. MainWindow::MainWindow(QWidget *parent) : //構造函數,初始化ui,計時器
  12. QMainWindow(parent),
  13. ui(new Ui::MainWindow)
  14. {
  15. ui->setupUi(this);
  16. timer = new QTimer(this);
  17. QWidget::connect( timer, SIGNAL( timeout() ), this, SLOT( timer_update_currentTabInfo() ) );
  18. QWidget::connect( ui->tabWidget_INFO, SIGNAL( currentChanged() ),
  19. this, SLOT( on_tabWidget_currentChanged() ) );
  20. timer->start(1000);
  21. }
  22. MainWindow::~MainWindow()
  23. {
  24. delete ui;
  25. delete timer;
  26. }
  27. void MainWindow::timer_update_currentTabInfo()
  28. {
  29. int index = ui->tabWidget_INFO->currentIndex();
  30. //定時器只刷新內存tab頁面,用於進度條動態顯示
  31. if (index == 0)
  32. {
  33. show_tabWidgetInfo(index);
  34. }
  35. }
  36. void MainWindow::show_tabWidgetInfo(int index)
  37. {
  38. QString tempStr; //讀取文件信息字元串
  39. QFile tempFile; //用於打開系統文件
  40. int pos; //讀取文件的位置
  41. if (index == 0) //內存資源
  42. {
  43. tempFile.setFileName("/proc/meminfo"); //打開內存信息文件
  44. if ( !tempFile.open(QIODevice::ReadOnly) )
  45. {
  46. QMessageBox::warning(this, tr("warning"), tr("The meminfo file can not open!"), QMessageBox::Yes);
  47. return ;
  48. }
  49. QString memTotal;
  50. QString memFree;
  51. QString memUsed;
  52. QString swapTotal;
  53. QString swapFree;
  54. QString swapUsed;
  55. int nMemTotal, nMemFree, nMemUsed, nSwapTotal, nSwapFree, nSwapUsed;
  56. while (1)
  57. {
  58. tempStr = tempFile.readLine();
  59. pos = tempStr.indexOf("MemTotal");
  60. if (pos != -1)
  61. {
  62. memTotal = tempStr.mid(pos 10, tempStr.length()-13);
  63. memTotal = memTotal.trimmed();
  64. nMemTotal = memTotal.toInt()/1024;
  65. }
  66. else if (pos = tempStr.indexOf("MemFree"

    ), pos != -1)
  67. {
  68. memFree = tempStr.mid(pos 9, tempStr.length()-12);
  69. memFree = memFree.trimmed();
  70. nMemFree = memFree.toInt()/1024;
  71. }
  72. else if (pos = tempStr.indexOf("SwapTotal"), pos != -1)
  73. {
  74. swapTotal = tempStr.mid(pos 11, tempStr.length()-14);
  75. swapTotal = swapTotal.trimmed();
  76. nSwapTotal = swapTotal.toInt()/1024;
  77. }
  78. else if (pos = tempStr.indexOf("SwapFree"), pos != -1)
  79. {
  80. swapFree = tempStr.mid(pos 10,tempStr.length()-13);
  81. swapFree = swapFree.trimmed();
  82. nSwapFree = swapFree.toInt()/1024;
  83. break;
  84. }
  85. }
  86. nMemUsed = nMemTotal - nMemFree;
  87. nSwapUsed = nSwapTotal - nSwapFree;
  88. memUsed = QString::number(nMemUsed, 10);
  89. swapUsed = QString::number(nSwapUsed, 10);

  90. memFree = QString::number(nMemFree, 10);
  91. memTotal = QString::number(nMemTotal, 10);
  92. swapFree = QString::number(nSwapFree, 10);
  93. swapTotal = QString::number(nSwapTotal, 10);
  94. ui->label_RAM_Used->setText(memUsed " MB");
  95. ui->label_RAM_Left->setText(memFree " MB");
  96. ui->label_RAM_Total->setText(memTotal " MB");
  97. ui->label_SWAP_Used->setText(swapUsed " MB");
  98. ui->label_SWAP_Left->setText(swapFree " MB");
  99. ui->label_SWAP_Total->setText(swapTotal " MB");
  100. ui->progressBar_RAM->setValue(nMemUsed*100/nMemTotal);
  101. ui->progressBar_SWAP->setValue(nSwapUsed*100/nSwapTotal);
  102. tempFile.close(); //關閉內存信息文件
  103. int tt = 2; //取2個點採樣計算cpu當前利用律
  104. int cpuInfo[2][7];
  105. int cpuTotal[2][2];

  106. while (tt)
  107. {
  108. tempFile.setFileName("/proc/stat"); //打開CPU使用狀態信息
  109. if ( !tempFile.open(QIODevice::ReadOnly) )
  110. {
  111. QMessageBox::warning(this, tr("warning"), tr("The stat file can not open!"), QMessageBox::Yes);
  112. return;
  113. }
  114. tempStr = tempFile.readLine();
  115. for (int i = 0; i < 7; i )
  116. {
  117. cpuInfo[2-tt][i] = tempStr.section(" ", i 1, i 1).toInt();
  118. cpuTotal[1][2-tt] = cpuInfo[2-tt][i];
  119. if (i == 3)
  120. {
  121. cpuTotal[0][2-tt] = cpuInfo[2-tt][i];
  122. }
  123. }
  124. tt--;
  125. tempFile.close(); //關閉stat文件
  126. }
  127. int a = cpuTotal[0][1] - cpuTotal[0][0];
  128. int b = cpuTotal[1][1] - cpuTotal[1][0];
  129. if (a < 0)
  130. {
  131. a = -a;
  132. }
  133. if (b < 0)
  134. {
  135. b = -b;
  136. }
  137. ui->progressBar_CPU->setValue(a*100/b);
  138. tempFile.setFileName("/proc/stat");
  139. if ( !tempFile.open(QIODevice::ReadOnly) )
  140. {
  141. QMessageBox::warning(this, tr("warning"), tr("The stat file can not open!"), QMessageBox::Yes);
  142. return;
  143. }
  144. tempStr = tempFile.readLine();
  145. a0 = a1;
  146. b0 = b1;
  147. a1 = b1 = 0;
  148. int gg;
  149. for (int i = 0; i < 7; i )

  150. {
  151. b1 = tempStr.section(" ", i 2, i 2).toInt();
  152. gg = b1;
  153. if (i == 3)
  154. {
  155. a1 = tempStr.section(" ", i 2, i 2).toInt();
  156. }
  157. }
  158. int m, n;
  159. m = a1 - a0;
  160. n = b1 - b0;
  161. if (m < 0)
  162. {
  163. m = -m;
  164. }
  165. if (n < 0)
  166. {
  167. n = -n;
  168. }
  169. ui->progressBar_CPU->setValue( (n-m)*100/n );
  170. tempFile.close(); //關閉stat文件
  171. }
  172. else if (index == 1) //進程信息
  173. {
  174. ui->listWidget_process->clear();
  175. QDir qd("/proc");
  176. QStringList qsList = qd.entryList();

  177. QString qs = qsList.join("n");
  178. QString id_of_pro;
  179. bool ok;
  180. int find_start = 3;
  181. int a, b;
  182. int nProPid; //進程PID
  183. int number_of_sleep = 0, number_of_run = 0, number_of_zombie = 0;
  184. int totalProNum = 0; //進程總數
  185. QString proName; //進程名
  186. QString proState; //進程狀態
  187. QString proPri; //進程優先順序
  188. QString proMem; //進程佔用內存
  189. QListWidgetItem *title = new QListWidgetItem("PIDt" QString::fromUtf8("名稱") "tt"
  190. QString::fromUtf8("狀態") "t"
  191. QString::fromUtf8(

    "優先順序") "t"
  192. QString::fromUtf8("佔用內存"), ui->listWidget_process);
  193. //循環讀取進程
  194. while (1)
  195. {
  196. //獲取進程PID
  197. a = qs.indexOf("n", find_start);
  198. b = qs.indexOf("n", a 1);
  199. find_start = b;
  200. id_of_pro = qs.mid(a 1, b-a-1);
  201. totalProNum ;
  202. nProPid = id_of_pro.toInt(&ok, 10);
  203. if(!ok)
  204. {
  205. break;
  206. }
  207. //打開PID所對應的進程狀態文件
  208. tempFile.setFileName("/proc/" id_of_pro "/stat");
  209. if ( !tempFile.open(QIODevice::ReadOnly) )
  210. {
  211. QMessageBox::warning(

    this, tr("warning"), tr("The pid stat file can not open!"), QMessageBox::Yes);
  212. return;
  213. }
  214. tempStr = tempFile.readLine();
  215. if (tempStr.length() == 0)
  216. {
  217. break;
  218. }
  219. a = tempStr.indexOf("(");
  220. b = tempStr.indexOf(")");
  221. proName = tempStr.mid(a 1, b-a-1);
  222. proName.trimmed(); //刪除兩端的空格
  223. proState = tempStr.section(" ", 2, 2);
  224. proPri = tempStr.section(" ", 17, 17);
  225. proMem = tempStr.section(" ", 22, 22);
  226. switch ( proState.at(0).toLatin1() )
  227. {
  228. case 'S': number_of_sleep ; break; //Sleep

  229. case 'R': number_of_run ; break; //Running
  230. case 'Z': number_of_zombie ; break; //Zombie
  231. default : break;
  232. }
  233. if (proName.length() >= 12)
  234. {
  235. QListWidgetItem *item = new QListWidgetItem(id_of_pro "t"
  236. proName "t"
  237. proState "t"
  238. proPri "t"
  239. proMem, ui->listWidget_process);
  240. }
  241. else
  242. {
  243. QListWidgetItem *item = new

    QListWidgetItem(id_of_pro "t"
  244. proName "tt"
  245. proState "t"
  246. proPri "t"
  247. proMem, ui->listWidget_process);
  248. }
  249. }
  250. QString temp;
  251. temp = QString::number(totalProNum, 10);
  252. ui->label_pNum->setText(temp);
  253. temp = QString::number(number_of_run, 10);
  254. ui->label_pRun->setText(temp);
  255. temp = QString::number(number_of_sleep, 10);
  256. ui->label_pSleep->setText(temp);
  257. temp = QString::number(number_of_zombie, 10);
  258. ui->label_pZombie->setText(temp);
  259. tempFile.close(); //關閉該PID進程的狀態文件
  260. }
  261. else if (index == 2) //模塊信息
  262. {
  263. ui->listWidget_model->clear();

  264. tempFile.setFileName("/proc/modules"); //打開模塊信息文件
  265. if ( !tempFile.open(QIODevice::ReadOnly) )
  266. {
  267. QMessageBox::warning(this, tr("warning"), tr("The modules file can not open!"), QMessageBox::Yes);
  268. return ;
  269. }
  270. //設置模塊首行項目
  271. QListWidgetItem *title = new QListWidgetItem( QString::fromUtf8("名稱") "ttt"
  272. QString::fromUtf8("使用內存數") "tt"
  273. QString::fromUtf8("使用次數"), ui->listWidget_model);
  274. QString mod_Name, mod_Mem, mod_Num;
  275. //循環讀取文件內容,查找需要的信息
  276. while (1)
  277. {
  278. tempStr = tempFile.readLine();

  279. if (tempStr.length() == 0)
  280. {
  281. break;
  282. }
  283. mod_Name = tempStr.section(" ", 0, 0);
  284. mod_Mem = tempStr.section(" ", 1, 1);
  285. mod_Num = tempStr.section(" ", 2, 2);
  286. if (mod_Name.length() > 10)
  287. {
  288. QListWidgetItem *item = new QListWidgetItem(mod_Name "tt"
  289. mod_Mem "tt"
  290. mod_Num, ui->listWidget_model);
  291. }
  292. else
  293. {
  294. QListWidgetItem *item = new QListWidgetItem(mod_Name "ttt"
  295. mod_Mem "tt"
  296. mod_Num, ui->listWidget_model);

  297. }
  298. }
  299. tempFile.close(); //關閉模塊信息文件
  300. }
  301. else if (index == 3) //系統信息
  302. {
  303. //int ok;
  304. tempFile.setFileName("/proc/cpuinfo"); //打開CPU信息文件
  305. if ( !tempFile.open(QIODevice::ReadOnly) )
  306. {
  307. QMessageBox::warning(this, tr("warning"), tr("The cpuinfo file can not open!"), QMessageBox::Yes);
  308. return;
  309. }
  310. //循環讀取文件內容,查找需要的信息
  311. while (1)
  312. {
  313. tempStr = tempFile.readLine();
  314. pos = tempStr.indexOf("model name");
  315. if (pos != -1)
  316. {

  317. pos = 13; //跳過前面的"model name:"所佔用的字元
  318. QString *cpu_name = new QString( tempStr.mid(pos, tempStr.length()-13) );
  319. ui->label_CPUName->setText(*cpu_name);
  320. }
  321. else if (pos = tempStr.indexOf("vendor_id"), pos != -1)
  322. {
  323. pos = 12; //跳過前面的"vendor_id:"所佔用的字元
  324. QString *cpu_type = new QString( tempStr.mid(pos, tempStr.length()-12) );
  325. ui->label_CPUType->setText(*cpu_type);
  326. }
  327. else if (pos = tempStr.indexOf("cpu MHz"), pos != -1)
  328. {
  329. pos = 11; //跳過前面的"cpu MHz:"所佔用的字元
  330. QString *cpu_frq = new QString( tempStr.mid(pos, tempStr.length()-11) );
  331. double cpufrq = cpu_frq->toDouble(); //4核CPU
  332. cpu_frq->setNum(cpufrq*4);
  333. ui->label_CPUFrequency->setText(*cpu_frq " HZ");
  334. }
  335. else if (pos = tempStr.indexOf("cache size"), pos!=-1)
  336. {
  337. pos = 13; //跳過前面的"cache size:"所佔用的字元
  338. QString *cache_size = new QString( tempStr.mid(pos, tempStr.length()-16) );
  339. int cachesize = cache_size->toInt(); //4核CPU
  340. cache_size->setNum(cachesize*4);
  341. ui->label_CatheCapacity->setText(*cache_size " KB");
  342. }
  343. else //跳過其他的內容
  344. {
  345. }
  346. }
  347. tempFile.close(); //關閉CPU信息文件
  348. //打開操作系統信息文件
  349. tempFile.setFileName("/proc/version");

  350. if ( !tempFile.open(QIODevice::ReadOnly) )
  351. {
  352. QMessageBox::warning(this, tr("warning"), tr("The version file can not open!"), QMessageBox::Yes);
  353. return ;
  354. }
  355. tempStr = tempFile.readLine();
  356. pos = tempStr.indexOf("version");
  357. QString *os_version = new QString( tempStr.mid(0, pos-1) );
  358. ui->label_SystemType->setText(*os_version);
  359. int pos1 = tempStr.indexOf("(");
  360. QString *os_type = new QString( tempStr.mid(pos, pos1-pos-1) );
  361. ui->label_SystemVersion->setText(*os_type);
  362. pos = tempStr.indexOf("gcc version");
  363. pos1 = tempStr.indexOf("#");
  364. QString *gcc_info = new QString( tempStr.mid(pos 12, pos1-pos-14) );
  365. ui->label_GCCVersion->setText(*gcc_info);
  366. tempFile.close(); //關閉操作系統信息文件
  367. }
  368. else //說明
  369. {
  370. }
  371. return;
  372. }
  373. void MainWindow::on_pushButton_halt_clicked()
  374. {
  375. system("halt");
  376. }
  377. void MainWindow::on_pushButton_reboot_clicked()
  378. {
  379. system("reboot");
  380. }
  381. void MainWindow::on_tabWidget_INFO_currentChanged(int index)
  382. {
  383. show_tabWidgetInfo(index); //顯示tab中的內容
  384. return ;
  385. }
  386. void MainWindow::on_pushButton_pkill_clicked()
  387. {
  388. //獲得進程號
  389. QListWidgetItem *item = ui->listWidget_process->currentItem();

  390. QString pro = item->text();
  391. pro = pro.section("t", 0, 0);
  392. system("kill " pro.toLatin1());
  393. QMessageBox::warning(this, tr("kill"), QString::fromUtf8("該進程已被殺死!"), QMessageBox::Yes);
  394. //回到進程信息tab表
  395. show_tabWidgetInfo(1);
  396. }
  397. void MainWindow::on_pushButton_prefresh_clicked()
  398. {
  399. show_tabWidgetInfo(1);
  400. }
  401. void MainWindow::on_pushButton_Model_install_clicked()
  402. {
  403. show_tabWidgetInfo(2); //安裝模塊還不知道如何實現
  404. }
  405. void MainWindow::on_pushButton_Model_remove_clicked()
  406. {
  407. show_tabWidgetInfo(2); //卸載模塊還不知道如何實現
  408. }
  409. void MainWindow::on_pushButton_Model_refresh_clicked()
  410. {
  411. show_tabWidgetInfo(2);

  412. }

本文出自 「rangercyh的分享空間」 博客,請務必保留此出處http://rangercyh.blog.51cto.com/1444712/521262


[火星人 ] linux系統Qt實現簡單的任務管理器已經有863次圍觀

http://coctec.com/docs/linux/show-post-49167.html