Qt linux query cpu usage

main.cpp

    #include "mainwindow.h"
    #include <QApplication>
    #include <QThread>
     
    Parser myParser;
     
    class MyFirstThread : public QThread
    {
    private:
        void run()
        {
            myParser.runParser();
        }
    };
     
     
     
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
     
        MyFirstThread t1;
        t1.start();
     
        QObject::connect(&myParser, SIGNAL(parserSignal(int)), &w, SLOT(usageSlot(int)));
     
     
        return a.exec();
    }

mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
     
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
     
    MainWindow::~MainWindow()
    {
        delete ui;
    }
     
    void MainWindow::usageSlot(int iUsage)
    {
        QString usageString = QString::number(iUsage);
        ui->label->setText(usageString);
    }

mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
     
    #include <QMainWindow>
    #include <QProcess>
    #include <QDebug>
    #include <QPushButton>
     
    namespace Ui {
    class MainWindow;
    }
     
    class Parser : public QObject
    {
        Q_OBJECT
     
    signals:
        void parserSignal(int iUsage);
     
    public:
        void runParser()
        {
            QProcess p;
            int prev_total = 0;
            int prev_idle = 0;
            while(1)
                    {
                        p.start("sh", QStringList() << "-c" << "cat /proc/stat | head -n1 | sed 's/cpu //'");
                        p.waitForFinished(-1);
     
                        QString p_stdout = p.readAllStandardOutput();
     
                        QStringList list1 = p_stdout.split(" ");
     
                        qDebug() << p_stdout;
     
                        int user = list1[1].toInt();
                        int system = list1[2].toInt();
                        int nice = list1[3].toInt();
                        int idle = list1[4].toInt();
                        int wait = list1[5].toInt();
                        int irq = list1[6].toInt();
                        int srq = list1[7].toInt();
                        int zero = list1[8].toInt();
     
                        int total=( user + system + nice + idle + wait + irq + srq + zero);
     
                        int diff_idle = idle-prev_idle;
     
                        int diff_total = (total-prev_total);
     
                        int usage=((( 1000 * ( (diff_total - diff_idle)) / diff_total+5) ) / 10);
     
                        qDebug() << "CPU: " << usage;
                        emit parserSignal(usage);
     
                        prev_total = total;
                        prev_idle = idle;
                        sleep (1);
                    }
        }
    };
     
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
     
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
     
    private:
        Ui::MainWindow *ui;
     
    public slots:
        void usageSlot(int iUsage);
    };
     
    #endif // MAINWINDOW_H

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>