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

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

2:把qDebug信息保存到本地

3:执行shell脚本

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

先上代码:

#include "logbrowser.h"

#include

#include

#include

QPointer 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

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

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()<

}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();

}

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

shell 执行qt生成文件_QT-窗口打印debug信息,本地日志保存,以及执行shell脚本并且把信息打印在窗口...相关推荐

  1. linux开发板添加qt库文件_QT程序怎么移植到开发板并运行

    需要的条件: 1.上位机编译好的可执行文件,以hello为例 在虚拟机的Ubuntu下,写好代码之后,定位到当前目录,执行命令 $ qmake -project //生成工程文件.pro $ qmak ...

  2. anaconda如何保存python文件_想在Jupyter Notebook(Anaconda)中保存并运行Python脚本

    确保您的ipython笔记本与python脚本位于同一文件夹中.此外,您可能必须在与python脚本相同的文件夹中创建一个空的__init__.py文件,以使导入工作. 由于您可能正在修改您的pyth ...

  3. linux启动 profile,Linux 启动时profile、bashrc、~/.bash_profile、~/.bashrc、~/.bash_profile执行顺序以及文件说明...

    Linux 启动时profile.bashrc.~/.bash_profile.~/.bashrc.~/.bash_profile执行顺序以及文件说明 一.执行顺序 登录linux时,/etc/pro ...

  4. maven 执行testng.xml文件失败解决问题

    maven 执行testng.xml文件失败解决问题 参考文章: (1)maven 执行testng.xml文件失败解决问题 (2)https://www.cnblogs.com/woniu123/p ...

  5. 5.Flink原理初探\角色分工\执行流程图生成\DataFlow,Operator,Partition,Parallelism,SubTask\OperatorChain和Task\任务槽\槽共享

    本文来自:Flink1.12-2021黑马程序员贺岁视频 的学习笔记 5.Flink原理初探 5.1.角色分工 5.2.执行流程 5.3.DataFlow 5.3.1.DataFlow.Operato ...

  6. 运行python程序的两种方式交互式和文件式_教你如何编写、保存与运行 Python 程序...

    第一步 接下来我们将看见如何在 Python 中运行一个传统的"Hello World"程序.Python教程本章将会教你如何编写.保存与运行 Python 程序. 通过 Pyth ...

  7. python解析log文件_python解析基于xml格式的日志文件

    大家中午好,由于过年一直还没回到状态,好久没分享一波小知识了,今天,继续给大家分享一波Python解析日志的小脚本. 首先,同样的先看看日志是个啥样. 都是xml格式的,是不是看着就头晕了??没事,我 ...

  8. JAVA调用Bartender进行标签打印(可本地用打印机客户端进行测试打印,【云上的项目】可通过WebSocket进行通讯进行打印)

    用Java编写一个打印标签客户端 点击运行启动会打开首页 可以点击预览打印 点击打印可测试成功 打印机结果 前端用的是thymeleaf 代码片段 <!DOCTYPE html> < ...

  9. qt生成无ui界面动态库,有ui界面的动态库,以及含有资源文件和qss文件的动态库

    提要 此文分别就qt生成纯代码的动态库,含有ui文件的动态库以及含有资源文件qss文件和切图的动态库. 实现 1.纯代码的生成qt库.即没有ui文件的项目. 打开QtCreate,新建文件,选择lib ...

  10. 使用Qt生成第一个窗口程序

    一.打开QtCreater,点击New Project 二.在Qt中,最常用的窗口程序为widgets控件程序,这里我们选择Qt Widgets Application 三.Qt生成的debug和re ...

最新文章

  1. 使用Office组件读取Excel,引用Microsoft.Office.Interop.Excel出现的问题
  2. Building Fire Stations 39届亚洲赛牡丹江站B题
  3. 机房收费系统中——存储过程中加入事务,实现学生注册
  4. 使用RawSocket进行网络抓包
  5. 台式电脑不拉网线上网_技巧知识:电脑不用网线也可以上网了,你知道吗?
  6. mysql group by having count_mysql中count(), group by, order by使用详解
  7. 第2课 - 搭建Lua开发环境
  8. springcloud 创建子父项目_idea搭建springCloud----搭建父子项目(二)
  9. C# 取二位小数点(四舍五入)
  10. 学习用Python编程时要避免的3个错误
  11. python视频教程全集-Python 3视频教程全集(2018版)免费送啦
  12. hbase shell基础和常用命令详解
  13. gunicorn: No module named 'fcntl'
  14. python实时监听微博发文同步到微信
  15. 【.Net Core】编译时禁止自动生成netcoreapp文件夹
  16. 纸的大小图解_纸张的尺寸标准
  17. hoolilaw特别分享:在美国喝多少酒就算酒驾
  18. Linux(Ubuntu)下使用OneNote
  19. Techstars携手Alphabit Fund与Launchpool,在伦敦推出专注于区块链的新加速器
  20. 肖特基二极管的反向恢复过程

热门文章

  1. [pl-slam] 几个重要的参数属性
  2. 2017.3.20-morning
  3. 关于信息安全工作方法论的一点猜想
  4. NET对象的XML序列化和反序列化
  5. Win10窗口拖动时自动最大化的问题,屏幕显示绿框,中间显示1
  6. 汇编语言数据段查找ASCII码并回显
  7. GEE 导入shp数据-裁剪影像
  8. HTML学习总结(6)——表单
  9. windows 64位PHP5.5配置xhprof
  10. android解压sd卡中的压缩文件