好久沒寫部落格了,最近工作需要,研究了一下下面這些功能:
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();}
一定要注意類與類之間的層次關係。