短网址生成站长工具,网站建设的费用入账,泉州做网站企业,wordpress文章留言QWidget实现了log日志的打印功能#xff0c;不仅可以在界面显示#xff0c;还可以生成打印日志。先来看下效果#xff0c;源码放在文章末尾#xff1a;
LogPlugin插件类管理log所有功能#xff0c;它可以获取Log界面并能打印正常信息#xff0c;警告信息和错误信息不仅可以在界面显示还可以生成打印日志。先来看下效果源码放在文章末尾
LogPlugin插件类管理log所有功能它可以获取Log界面并能打印正常信息警告信息和错误信息下面是它的接口描述
class LOGGINGWRAPPER_EXPORT LogPlugin
{
public:virtual ~LogPlugin() {}virtual void Info(std::string message) 0; //打印绿色正常信息virtual void Warn(std::string message) 0; //打印黄色警告信息virtual void Error(std::string message) 0; //打印红色错误信息virtual QWidget* GetLogWidget() 0; //获取Log界面virtual void SetParent(QWidget* widget) 0; //设置父窗口
};使用方法
1、创建LogPlugin对象 LogPlugin是QPlugin插件实现可以通过QPlugin读取LogPlugin.dll把读到的QObject指针强转为LogPlugin对象。插件的使用可以看之前的篇章。
LogPlugin* logPlugin qobject_castLogPlugin*(objectPtr);当然也可以直接new一个LogPlugin对象。
2、把Log界面放在收缩栏中 我把log界面放在了收缩栏中可以看到鼠标点击log界面可以收起再次点击可以展开收缩栏的实现前面文章已经讲解过这里不在赘述。QWidget实现收缩栏效果 CollpasePagePlugin* collpasePagePlugin qobject_castCollpasePagePlugin*(objectPtr_);if (logPlugin collpasePagePlugin){collpasePagePlugin-GetWidget()-setParent(ui.centralWidget);collpasePagePlugin-SetLayout(m_centerQVBoxLayout, -1); //Put the folded plug-in into the layout}上面代码先创建收缩栏对象并设置收缩栏的父窗口和垂直布局-1代表放置在收缩栏中的界面放在布局的最上层位置。
logPlugin-SetParent(collpasePagePlugin-GetWidget()); //Set Parent Class
QWidget* widget logPlugin-GetLogWidget();//Put the Logwidget into the folding plug-in
collpasePagePlugin-addWidget(widget, Log, QIcon(:/Gen2WGMTFTester/images/routine/log.jpg));SetParent把log界面的父窗口设为收缩栏并通过addWidget把Log窗口添加到收缩栏中显示
3、打印log信息
logPlugin-Info(green is normal);
logPlugin-Warn(yellow is warning);
logPlugin-Error(red is error);这里分别打印绿色正常信息黄色警告信息和红色错误信息。
想在项目下生成Log日志还需要做如下设置 1在mainwindow主程序目录下创建config文件夹并把logconfig.properites文件拷贝进去
2附加包含目录添加logplus头文件目录 3附加库目录添加logplus的lib目录 4附加依赖项添加debug或release的lib文件名 记得更改下logplus.cpp下面的lib路径
源码下载