qlogqt,在qml里面使用

文章提供了一个QmlLog4Qml类,用于在Qml中记录调试信息。类实现了将不同类型的日志消息(如QtDebugMsg和QtCriticalMsg)写入到指定文件的功能,并确保了线程安全。在main.cpp中注册该类的实例,以便在Qml中调用。
摘要由CSDN通过智能技术生成

发现前面只写了大概的log的逻辑而没有贴上代码,这里补上代码吧

. h的代码

#include <QObject>

class QmlLog4Qml : public QObject
{
    Q_OBJECT

public:
    QmlLog4Qml();
    Q_INVOKABLE void qDebug_Info(int type, QString strInfo);
};

. cpp代码

/*!
 *@file QmlLog4Qml.cpp
 *@brief Qml写日志
 *@version 1.0
 *@section LICENSE Copyright (C) 2003-2103 CamelSoft Corporation
 *@author zhengtianzuo
*/
#include "QmlLog4Qml.h"
#include <QMutex>
#include <QFile>
#include <QDateTime>
#include <QTextStream>


void outputMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    static QMutex mutex;
    mutex.lock();

    QString text;
    QString message = "";
    switch(type)
    {
    case QtDebugMsg: {
        text = QString("Debug:");
        if (context.file != nullptr)
        {
            QString context_info = QString("File:(%1) Line:(%2)").arg(QString(context.file)).arg(context.line);
            QString current_date_time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ddd");
            QString current_date = QString("(%1)").arg(current_date_time);
            message = QString("%1 %2 %3 %4").arg(text).arg(context_info).arg(msg).arg(current_date);
        }

        else
        {
            message = msg;
        }
        QFile file("configlog.txt");
        file.open(QIODevice::WriteOnly | QIODevice::Append);
        QTextStream text_stream(&file);
        text_stream << message << "\r\n";
        file.flush();
        file.close();
    }
        break;
     case QtCriticalMsg: {
        if (context.file != nullptr)
        {
            QString context_info = QString("File:(%1) Line:(%2)").arg(QString(context.file)).arg(context.line);
            QString current_date_time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ddd");
            QString current_date = QString("(%1)").arg(current_date_time);
            message = QString("%1 %2 %3 %4").arg(msg).arg(current_date);
        }

        else
         {
            message = msg;
        }
        QFile file("templog.txt");
        file.open(QIODevice::WriteOnly | QIODevice::Append);
        QTextStream text_stream(&file);
        text_stream << message << "\r\n";
        file.flush();
        file.close();
    }
        break;

    }

    mutex.unlock();
}

QmlLog4Qml::QmlLog4Qml()
{
    qInstallMessageHandler(outputMessage);
}

void QmlLog4Qml::qDebug_Info(int type, QString strInfo)
{
    QMessageLogContext context;
    context.file = nullptr;
    outputMessage((QtMsgType)type, context, strInfo);
}

还需要在main. cpp里面注册单例

 QmlLog4Qml log4Qml; QQmlApplicationEngine engine; engine.rootContext()->setContextProperty("log4Qml", &log4Qml);

qml里面使用:

可以写上调用filedialog的函数去打开文件

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值