Qt 中的信号与槽,连接日志库QsLog,在界面上显示出日志信息

33 篇文章 0 订阅

新建一个基于QWidget的工程,在界面上添加一个QTextBrowser控件,用来在界面显示日志信息。

在pro文件中添加QsLog的头文件和lib库

INCLUDEPATH += include/QsLog.h \
               include/QsLogDest.h \
               include/QsLogDestConsole.h \
               include/QsLogDestFile.h \
               include/QsLogDestFunctor.h \
               include/QsLogDisableForThisFile.h \
               include/QsLogLevel.h

LIBS += $$PWD/lib/QsLog2.lib

在widget.h中添加槽函数:

public slots:
    void logSlot(const QString &message, int level);

widget.h文件:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

    void initLogger();
    void destroyLogger();

public slots:
    void logSlot(const QString &message, int level);

private:
    Ui::Widget *ui;
};

#endif // WIDGET_H

widget.cpp文件:

#include "widget.h"
#include "ui_widget.h"

#include "include/QsLog.h"
#include "include/QsLogDest.h"

#include <QCoreApplication>
#include <QDir>
//#include <iostream>

using namespace QsLogging;

/*
void logFunction(const QString &message, QsLogging::Level level)
{
    std::cout << "From log function: " << qPrintable(message) << " " << static_cast<int>(level)
              << std::endl;
}
*/

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    initLogger();
}

Widget::~Widget()
{
    delete ui;

    destroyLogger();
}

void Widget::initLogger()
{
    // 1. init the logging mechanism
    Logger& logger = Logger::instance();
    logger.setLoggingLevel(QsLogging::TraceLevel);
    //设置log位置
    const QString sLogPath(QDir(QCoreApplication::applicationDirPath()).filePath("log.txt"));

    // 2. add two destinations
    DestinationPtr fileDestination(DestinationFactory::MakeFileDestination(
      sLogPath, EnableLogRotation, MaxSizeBytes(512), MaxOldLogCount(2)));
    DestinationPtr debugDestination(DestinationFactory::MakeDebugOutputDestination());
    //DestinationPtr functorDestination(DestinationFactory::MakeFunctorDestination(&logFunction));

    //这样和槽函数连接
    DestinationPtr sigsSlotDestination(DestinationFactory::MakeFunctorDestination(this, SLOT(logSlot(QString,int))));

    logger.addDestination(debugDestination);
    logger.addDestination(fileDestination);
    //logger.addDestination(functorDestination);
    logger.addDestination(sigsSlotDestination);

    // 3. start logging
    QLOG_INFO() << "Program started";
    QLOG_INFO() << "Built with Qt" << QT_VERSION_STR << "running on" << qVersion();

    QLOG_TRACE() << "Here's a" << QString::fromUtf8("trace") << "message";
    QLOG_DEBUG() << "Here's a" << static_cast<int>(QsLogging::DebugLevel) << "message";
    QLOG_WARN()  << "Uh-oh!";
    qDebug() << "This message won't be picked up by the logger";
    QLOG_ERROR() << "An error has occurred";
    qWarning() << "Neither will this one";
    QLOG_FATAL() << "Fatal error!";
}

void Widget::destroyLogger()
{
    QsLogging::Logger::destroyInstance();
}

void Widget::logSlot(const QString &message, int level)
{
    ui->textBrowser->append(qPrintable(message));
}

运行效果:
在这里插入图片描述
查看函数调用过程:

DestinationPtr DestinationFactory::MakeFunctorDestination(QsLogging::Destination::LogFunction f)
{
    return DestinationPtr(new FunctorDestination(f));
}

stinationPtr DestinationFactory::MakeFunctorDestination(QObject *receiver, 
														const char *member)
{
    return DestinationPtr(new FunctorDestination(receiver, member));
}

继续看FunctorDestination的构造函数:
只有两个构造函数

  	explicit FunctorDestination(LogFunction f);
    FunctorDestination(QObject *receiver, const char *member);

第二个构造函数,找到了连接信号与槽的地方,这就是我们需要的函数。

QsLogging::FunctorDestination::FunctorDestination(QObject *receiver, const char *member)
    : QObject(NULL)
    , mLogFunction(NULL)
{
    connect(this, SIGNAL(logMessageReady(QString,int)), receiver, member, Qt::QueuedConnection);
}

demo工程代码:https://download.csdn.net/download/hp_cpp/10757836

  • 1
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值