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

Qt 中的信号与槽,连接日志库QsLog,在界面上显示出日志信息
            
            
                
                                                                                                    2018年11月01日 12:25:34
                    hp_cpp
                        阅读数:274
                        
                                                                                                                            
                
                                    
            
        
    
    
        
                                
                                      
                                      
                    版权声明:本文为博主原创文章,未经博主允许不得转载。                    https://blog.csdn.net/hp_cpp/article/details/83618395                
                                
                                                    
                            
                            
                            新建一个基于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
123456789
在widget.h中添加槽函数:
public slots:
    void logSlot(const QString &message, int level);

123
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

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
运行效果:

查看函数调用过程:
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));
}
12345678910
继续看FunctorDestination的构造函数:
只有两个构造函数
      explicit FunctorDestination(LogFunction f);
    FunctorDestination(QObject *receiver, const char *member);

123
第二个构造函数,找到了连接信号与槽的地方,这就是我们需要的函数。
QsLogging::FunctorDestination::FunctorDestination(QObject *receiver, const char *member)
    : QObject(NULL)
    , mLogFunction(NULL)
{
    connect(this, SIGNAL(logMessageReady(QString,int)), receiver, member, Qt::QueuedConnection);
}
123456
demo工程代码:https://download.csdn.net/download/hp_cpp/10757836
--------------------- 
作者:hp_cpp 
来源:CSDN 
原文:https://blog.csdn.net/hp_cpp/article/details/83618395 
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt Designer使用C++通过按钮、信号实现界面切换,需要按照以下步骤进行操作: 1. 在Qt Designer创建两个窗口,分别为窗口A和窗口B,分别保存为.ui文件。 2. 在窗口A添加一个按钮,命名为"切换到窗口B",并为其添加一个信号-连接,将其Clicked()信号连接到一个函数。 3. 在窗口B添加一个按钮,命名为"切换到窗口A",并为其添加一个信号-连接,将其Clicked()信号连接到一个函数。 4. 在C++创建一个类,继承自QMainWindow,命名为MainWindow,用于管理窗口A和窗口B的显示。 5. 在MainWindow类,定义两个成员变量,分别为窗口A和窗口B的指针。 6. 在MainWindow类的构造函数,通过ui文件加载窗口A和窗口B,并将其保存到成员变量。 7. 在MainWindow类,声明两个函数,分别为switchToWindowA和switchToWindowB,用于实现窗口的切换。 8. 在switchToWindowA函数,将窗口B隐藏,将窗口A显示来。 9. 在switchToWindowB函数,将窗口A隐藏,将窗口B显示来。 10. 在MainWindow类的构造函数,为按钮添加信号-连接,将切换到窗口A的按钮的Clicked()信号连接到switchToWindowA函数,将切换到窗口B的按钮的Clicked()信号连接到switchToWindowB函数。 11. 在main函数,创建MainWindow对象,并显示来。 示例代码如下: ```cpp #include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); windowA = new QWidget; windowB = new QWidget; ui->setupUi(windowA); ui->setupUi(windowB); connect(ui->switchToWindowAButton, SIGNAL(clicked()), this, SLOT(switchToWindowA())); connect(ui->switchToWindowBButton, SIGNAL(clicked()), this, SLOT(switchToWindowB())); } MainWindow::~MainWindow() { delete ui; } void MainWindow::switchToWindowA() { windowB->hide(); windowA->show(); } void MainWindow::switchToWindowB() { windowA->hide(); windowB->show(); } int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值