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

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

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

1:把qDebug信息打印到QT 窗口

2:把qDebug信息保存到本地

3:执行shell脚本

4:把终端信息输出到QT窗口


先上代码

  1. #include "logbrowser.h"
  2. #include <QApplication>
  3. #include <QPointer>
  4. #include <QDebug>
  5. QPointer<LogBrowser> log_broswer;
  6. void myMessageOutput(QtMsgType type,const char *msg)
  7. {
  8. if(log_broswer)
  9. log_broswer->outputMessage(type,msg);
  10. }
  11. int main(int argc,char *argv[])
  12. {
  13. QApplication a(argc,argv);
  14. log_broswer = new LogBrowser;
  15. log_broswer->show();
  16. qInstallMsgHandler(myMessageOutput);
  17. int result = a.exec();
  18. delete log_broswer;
  19. return result;
  20. }



  1. #ifndef LOGBROWSER_H
  2. #define LOGBROWSER_H
  3. #include <QWidget>
  4. #include <QTextBrowser>
  5. #include <QPushButton>
  6. #include <QHBoxLayout>
  7. #include <QVBoxLayout>
  8. #include <QtDebug>
  9. #include <QMessageBox>
  10. #include <QCloseEvent>
  11. #include <QProcess>
  12. #include <QFileSystemWatcher>
  13. #include <QDir>
  14. #include <QTimer>
  15. namespace Ui {
  16. class LogBrowser;
  17. }
  18. class LogBrowser : public QWidget
  19. {
  20. Q_OBJECT
  21. public:
  22. explicit LogBrowser(QWidget *parent = 0);
  23. ~LogBrowser();
  24. void outputMessage(QtMsgType type,const QString &msg);
  25. public slots:
  26. void start();
  27. void save(bool enable);
  28. void directoryUpdated(const QString &path); // 目录更新时调用,path是监控的路径
  29. private slots:
  30. void on_pushButton_start_clicked();
  31. void on_pushButton_stop_clicked();
  32. void on_pushButton_save_clicked();
  33. void on_pushButton_exit_clicked();
  34. void autoUpdata();
  35. void on_pushButton_updata_clicked();
  36. void displayUdiskFileList();
  37. void readCmdInformation();
  38. private:
  39. Ui::LogBrowser *ui;
  40. QTextBrowser *browser;
  41. QPushButton *start_button;
  42. QPushButton *clear_button;
  43. QProcess *my_process;
  44. QFileSystemWatcher *my_sysWatcher;
  45. QTimer *my_timer;
  46. bool is_finished;
  47. bool my_saveEnable;
  48. };
  49. #endif // LOGBROWSER_H




  1. #include "logbrowser.h"
  2. #include "ui_logbrowser.h"
  3. LogBrowser::LogBrowser(QWidget *parent) :
  4. QWidget(parent),ui(new Ui::LogBrowser)
  5. {
  6. ui->setupUi(this);
  7. //背景透明
  8. setAutoFillBackground(false);
  9. setWindowFlags(Qt::FramelessWindowHint);
  10. setAttribute(Qt::WA_TranslucentBackground,true);
  11. my_timer = new QTimer();
  12. my_process = new QProcess(); //执行命令的进程
  13. //当有输出时,发出消息。接收槽会读取进程管道内的数据
  14. connect(my_process,SIGNAL(readyRead()),this,SLOT(readCmdInformation()));
  15. my_sysWatcher = new QFileSystemWatcher();
  16. my_sysWatcher->addPath("/mnt"); //监控文件夹路径
  17. //如果文件夹有变动,则发出通知
  18. connect( my_sysWatcher,SIGNAL(directoryChanged(QString)),SLOT(directoryUpdated(QString)));
  19. ui->pushButton_updata->setEnabled(false);
  20. is_finished = false;
  21. my_saveEnable = false;
  22. }
  23. LogBrowser::~LogBrowser()
  24. {
  25. delete ui;
  26. }
  27. //! 读取命令窗口的信息,显示在自己的窗口上
  28. void LogBrowser::readCmdInformation()
  29. {
  30. QString str = my_process->readAllStandardOutput();
  31. ui->plainTextEdit->appendPlainText( str );
  32. ui->plainTextEdit->moveCursor(QTextCursor::End);
  33. }
  34. //!接收debug 底层信息
  35. void LogBrowser::outputMessage(QtMsgType type,const QString &msg)
  36. {
  37. QString message;
  38. switch(type)
  39. {
  40. case QtDebugMsg:
  41. message = QString("Debug:");
  42. break;
  43. case QtWarningMsg:
  44. message = QString("Warning:");
  45. break;
  46. case QtCriticalMsg:
  47. message = QString("Critical:");
  48. break;
  49. case QtFatalMsg:
  50. message = QString("Fatal:");
  51. break;
  52. }
  53. ui->plainTextEdit->appendPlainText(message.append(msg) );
  54. ui->plainTextEdit->moveCursor(QTextCursor::End); //滑动条移动到底端
  55. if( my_saveEnable )
  56. {
  57. QFile file("log.txt");
  58. file.open(QIODevice::WriteOnly | QIODevice::Append);
  59. QTextStream text_stream(&file);
  60. text_stream << message << "\r\n";
  61. file.flush();
  62. file.close();
  63. }
  64. }
  65. //一个测试函数,正常使用时,可以删除
  66. void LogBrowser::start()
  67. {
  68. for(int i=0; i<1000000; i++)
  69. {
  70. if(!is_finished)
  71. {
  72. QCoreApplication::processEvents();
  73. qDebug()<<QString("qDebug::").append(QString::number(i,10));
  74. }else{
  75. return;
  76. }
  77. }
  78. }
  79. //!打开保存开关,则所有DEBUG信息保存到log.txt文件
  80. void LogBrowser::save(bool enable)
  81. {
  82. my_saveEnable = enable;
  83. }
  84. //! 自动升级,也就是自动执行脚本。
  85. void LogBrowser::autoUpdata()
  86. {
  87. ui->plainTextEdit->appendPlainText("Auto UpData start!!!\r\n" );
  88. my_process->start("/mnt/sda1/auto.sh");
  89. // my_process->start("/home/lbs_work/test.sh");
  90. }
  91. //! 当有U盘插入时,则读取文件目录,并打开更新
  92. void LogBrowser::directoryUpdated(const QString &path)
  93. {
  94. const QDir dir(path);
  95. QStringList newEntryList = dir.entryList(QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Files,QDir::DirsFirst);
  96. if( newEntryList.contains("sda1"))
  97. {
  98. ui->plainTextEdit->setPlainText("U disk has been mounted");
  99. emit my_timer->singleShot(2000,SLOT(displayUdiskFileList()));
  100. ui->pushButton_updata->setEnabled( true );
  101. }else{
  102. ui->plainTextEdit->setPlainText("U disk has been unmounted");
  103. ui->pushButton_updata->setEnabled(false);
  104. }
  105. }
  106. //! 显示U盘文件夹目录,这里需要延时,因为刚挂载U盘,马上读取是读不到任何东西的
  107. void LogBrowser::displayUdiskFileList()
  108. {
  109. const QDir udir("/mnt/sda1");
  110. QStringList uDiskList = udir.entryList(QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Files,QDir::DirsFirst);
  111. ui->plainTextEdit->appendPlainText(QString().setNum(uDiskList.length()));
  112. for( int i = 0; i < uDiskList.length(); ++i )
  113. {
  114. ui->plainTextEdit->appendPlainText( uDiskList[i] );
  115. }
  116. }
  117. void LogBrowser::on_pushButton_start_clicked()
  118. {
  119. is_finished = false;
  120. this->start();
  121. }
  122. void LogBrowser::on_pushButton_stop_clicked()
  123. {
  124. is_finished = true;
  125. }
  126. void LogBrowser::on_pushButton_save_clicked()
  127. {
  128. this->save(true);
  129. }
  130. void LogBrowser::on_pushButton_exit_clicked()
  131. {
  132. this->close();
  133. delete this; //这是重点
  134. }
  135. void LogBrowser::on_pushButton_updata_clicked()
  136. {
  137. this->autoUpdata();
  138. }

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

猜你在找的Bash相关文章