• Qt 日志界面类


    1 UI界面文件为QTablewidget

    2 和spdlog结合使用

    3 头文件

    #ifndef _SIMLOGWIDGET_H_
    #define _SIMLOGWIDGET_H_
    
    #include <QtWidgets/QWidget>
    #include "ui_simlog.h"
    
    
    
    //日志等级
    enum LogLevel
    {
        KLogTrace = 0,
        KLogDebug,
        KLogInfo,
        KLogWarn,
        KLogErr,
        KLogCritical,
    };
    
    
    
    class  LogWidget : public QWidget
    {
        Q_OBJECT
    
    public:
        static LogWidget& Instance();
        void Log(LogLevel level, std::string data,bool writefile = false);
    private:
        LogWidget(QWidget *parent = Q_NULLPTR);
        Ui::SimLogClass ui;
    
        int level_;
    };
    
    //writefile表示是否写入文件,默认不写文件
    void LOGUI(LogLevel level, std::string data, bool writefile = false);
    
    #endif //_SIMLOGWIDGET_H_

    4 cpp文件

    #include "simlogwidget.h"
    
    #include <QDateTime>
    #include <QDebug>
    #include <QString>
    #include "simlog.h"
    
    void LOGUI(LogLevel level, std::string data, bool writefile)
    {
        LogWidget::Instance().Log(level, data, writefile);
    }
    
    LogWidget::LogWidget(QWidget *parent): QWidget(parent)
    {
        ui.setupUi(this);
        //设置表格最后一列自适应拉伸
        ui.log_widget_->setColumnWidth(0, 220);
        ui.log_widget_->setColumnWidth(1, 70);
        ui.log_widget_->horizontalHeader()->setStretchLastSection(true);
        //设置表格禁止编辑
        ui.log_widget_->setEditTriggers(QAbstractItemView::NoEditTriggers);
        //设置单击选择一行
        ui.log_widget_->setSelectionBehavior(QAbstractItemView::SelectRows);
        //设置只能选择一行,不能多行选中
        ui.log_widget_->setSelectionMode(QAbstractItemView::SingleSelection);
    
    }
    
    LogWidget& LogWidget::Instance()
    {
        static LogWidget *logwidget=new LogWidget;
        return *logwidget;
    }
    
    
    void LogWidget::Log(LogLevel level, std::string data,bool writefile)
    {
        QString s_level;
    
        if (level == KLogTrace)
        {
            if (writefile)
                LTrace(data);
            s_level = "trace";
        }
        else if (level == KLogDebug)
        {
            if (writefile)
                LDebug(data);
            s_level = "debug";
        }
        else if (level == KLogInfo)
        {
            if (writefile)
                LInfo(data);
            s_level = "info";
        }
        else if (level == KLogWarn)
        {
            if (writefile)
                LWarn(data);
            s_level = "warn";
        }
        else if (level == KLogErr)
        {
            if (writefile)
                if (writefile)
                    LError(data);
            s_level = "error";
        }
        else if (level == KLogCritical)
        {
            if (writefile)
                LCritical(data);
            s_level = "critical";
        }
    
        QDateTime current_date_time = QDateTime::currentDateTime();
        QString current_date = current_date_time.toString("yyyy-MM-dd hh:mm::ss.zzz");
    
        int row_count = ui.log_widget_->rowCount();
        ui.log_widget_->insertRow(row_count);
        QTableWidgetItem *item_time = new QTableWidgetItem(current_date);
        QTableWidgetItem *item_level = new QTableWidgetItem(s_level);
        QTableWidgetItem *item_data = new QTableWidgetItem(QString::fromStdString(data));
        ui.log_widget_->setItem(row_count, 0, item_time);
        ui.log_widget_->setItem(row_count, 1, item_level);
        ui.log_widget_->setItem(row_count, 2, item_data);
    
    
    }

    5 使用方法

    (1)包含头文件

    (2)调用函数,如下:

        for (int i = 0; i < 100; i++)
        {
            LOGUI(KLogDebug, "infomessage");
            LOGUI(KLogInfo, "errormessage");
        }

    6 日志格式为:时间-日志类别-日志信息

  • 相关阅读:
    SQL server 事务介绍,创建与使用
    DOM操作系列-01
    JS键盘事件
    Js获取当前日期时间及其它操作
    js中!!的作用
    js == 与 === 的区别[转]
    学习总结--Dom
    css历史
    javascript中 visibility和display的区别
    “==”和Equals区别
  • 原文地址:https://www.cnblogs.com/LuckCoder/p/11195856.html
Copyright © 2020-2023  润新知