QT-窗口打印debug信息,本地日志保存,以及执行shell脚本并且把信息打印在窗口

前端之家收集整理的这篇文章主要介绍了QT-窗口打印debug信息,本地日志保存,以及执行shell脚本并且把信息打印在窗口前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

好久没写博客了,最近工作需要,研究了一下下面这些功能

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_OBJECT

public:
    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)),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");
}

//! 当有U盘插入时,则读取文件目录,并打开更新
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,SLOT(displayUdiskFileList()));
        ui->pushButton_updata->setEnabled( true );
    }else{
        ui->plainTextEdit->setPlainText("U disk has been unmounted");
        ui->pushButton_updata->setEnabled(false);
    }

}

//! 显示U盘文件夹目录,这里需要延时,因为刚挂载U盘,马上读取是读不到任何东西的
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();
}




一定要注意类与类之间的层次关系。

猜你在找的Bash相关文章