QT-視窗列印debug資訊,本地日誌儲存,以及執行shell指令碼並且把資訊列印在視窗

來源:互聯網
上載者:User

好久沒寫部落格了,最近工作需要,研究了一下下面這些功能:

1:把qDebug資訊列印到QT 視窗

2:把qDebug資訊儲存到本地

3:執行shell指令碼

4:把終端資訊輸出到QT視窗


先上代碼:

#include "logbrowser.h"#include <QApplication>#include <QPointer>#include <QDebug>QPointer<LogBrowser> log_broswer; void myMessageOutput(QtMsgType type, const char *msg){    if(log_broswer)    log_broswer->outputMessage(type, msg);}int main(int argc, char *argv[]){    QApplication a(argc, argv);    log_broswer = new LogBrowser;    log_broswer->show();    qInstallMsgHandler(myMessageOutput);    int result = a.exec();    delete log_broswer;    return result;}



#ifndef LOGBROWSER_H#define LOGBROWSER_H#include <QWidget>#include <QTextBrowser>#include <QPushButton>#include <QHBoxLayout>#include <QVBoxLayout>#include <QtDebug>#include <QMessageBox>#include <QCloseEvent>#include <QProcess>#include <QFileSystemWatcher>#include <QDir>#include <QTimer>namespace Ui {class LogBrowser;}class LogBrowser : public QWidget{    Q_OBJECTpublic:    explicit LogBrowser(QWidget *parent = 0);    ~LogBrowser();    void outputMessage(QtMsgType type,  const QString &msg);public slots:    void start();    void save(bool enable);    void directoryUpdated(const QString &path);  // 目錄更新時調用,path是監控的路徑private slots:    void on_pushButton_start_clicked();    void on_pushButton_stop_clicked();    void on_pushButton_save_clicked();    void on_pushButton_exit_clicked();    void autoUpdata();    void on_pushButton_updata_clicked();    void displayUdiskFileList();    void readCmdInformation();private:    Ui::LogBrowser *ui;    QTextBrowser *browser;    QPushButton *start_button;    QPushButton *clear_button;    QProcess *my_process;    QFileSystemWatcher *my_sysWatcher;    QTimer *my_timer;    bool is_finished;    bool my_saveEnable;};#endif // LOGBROWSER_H




#include "logbrowser.h"#include "ui_logbrowser.h"LogBrowser::LogBrowser(QWidget *parent) :    QWidget(parent),    ui(new Ui::LogBrowser){    ui->setupUi(this);    //背景透明    setAutoFillBackground(false);    setWindowFlags(Qt::FramelessWindowHint);    setAttribute(Qt::WA_TranslucentBackground,true);    my_timer = new QTimer();    my_process = new QProcess();                //執行命令的進程    //當有輸出時,發出訊息。接收槽會讀取進程管道內的資料    connect(my_process,SIGNAL(readyRead()),this,SLOT(readCmdInformation()));    my_sysWatcher = new QFileSystemWatcher();    my_sysWatcher->addPath("/mnt");             //監控檔案夾路徑    //如果檔案夾有變動,則發出通知    connect( my_sysWatcher,SIGNAL(directoryChanged(QString)),this,SLOT(directoryUpdated(QString)));    ui->pushButton_updata->setEnabled(false);    is_finished = false;    my_saveEnable = false;}LogBrowser::~LogBrowser(){    delete ui;}//! 讀取命令視窗的資訊,顯示在自己的視窗上void LogBrowser::readCmdInformation(){    QString str =  my_process->readAllStandardOutput();    ui->plainTextEdit->appendPlainText( str );    ui->plainTextEdit->moveCursor(QTextCursor::End);}//!接收debug 底層資訊void LogBrowser::outputMessage(QtMsgType type,  const QString &msg){    QString message;    switch(type)    {        case QtDebugMsg:            message = QString("Debug:");            break;        case QtWarningMsg:            message = QString("Warning:");            break;        case QtCriticalMsg:            message = QString("Critical:");            break;        case QtFatalMsg:            message = QString("Fatal:");            break;    }    ui->plainTextEdit->appendPlainText(message.append(msg) );    ui->plainTextEdit->moveCursor(QTextCursor::End);  //滑動條移動到底端    if( my_saveEnable )    {        QFile file("log.txt");        file.open(QIODevice::WriteOnly | QIODevice::Append);        QTextStream text_stream(&file);        text_stream << message << "\r\n";        file.flush();        file.close();    }}//一個測試函數,正常使用時,可以刪除void LogBrowser::start(){    for(int i=0; i<1000000; i++)    {        if(!is_finished)        {            QCoreApplication::processEvents();            qDebug()<<QString("qDebug::").append(QString::number(i, 10));        }else{            return;        }    }}//!開啟儲存開關,則所有DEBUG資訊儲存到log.txt檔案中void LogBrowser::save(bool enable){    my_saveEnable = enable;}//! 自動升級,也就是自動執行指令碼。void LogBrowser::autoUpdata(){    ui->plainTextEdit->appendPlainText("Auto UpData start!!!\r\n" );    my_process->start("/mnt/sda1/auto.sh");   // my_process->start("/home/lbs_work/test.sh");}//! 當有隨身碟插入時,則讀取檔案目錄,並開啟更新void LogBrowser::directoryUpdated(const QString &path){   const QDir dir(path);   QStringList newEntryList = dir.entryList(QDir::NoDotAndDotDot  | QDir::AllDirs | QDir::Files, QDir::DirsFirst);    if( newEntryList.contains("sda1"))    {        ui->plainTextEdit->setPlainText("U disk has been mounted");        emit my_timer->singleShot(2000,this,SLOT(displayUdiskFileList()));        ui->pushButton_updata->setEnabled( true );    }else{        ui->plainTextEdit->setPlainText("U disk has been unmounted");        ui->pushButton_updata->setEnabled(false);    }}//! 顯示隨身碟檔案夾目錄,這裡需要延時,因為剛掛載隨身碟,馬上讀取是讀不到任何東西的void LogBrowser::displayUdiskFileList(){    const QDir udir("/mnt/sda1");    QStringList uDiskList = udir.entryList(QDir::NoDotAndDotDot  | QDir::AllDirs | QDir::Files, QDir::DirsFirst);    ui->plainTextEdit->appendPlainText(QString().setNum(uDiskList.length()));    for( int i = 0; i < uDiskList.length(); ++i )    {        ui->plainTextEdit->appendPlainText( uDiskList[i] );    }}void LogBrowser::on_pushButton_start_clicked(){   is_finished = false;   this->start();}void LogBrowser::on_pushButton_stop_clicked(){    is_finished = true;}void LogBrowser::on_pushButton_save_clicked(){    this->save(true);}void LogBrowser::on_pushButton_exit_clicked(){    this->close();    delete this; //這是重點}void LogBrowser::on_pushButton_updata_clicked(){    this->autoUpdata();}

一定要注意類與類之間的層次關係。



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.