我是Qt/QML主题的新手,我正试图在我的c++业务逻辑中安装日志处理程序。下面的代码片段安装了一个处理程序并设置了一个特殊的类别:
int main(int argc, char *argv[])
{
qInstallMessageHandler(myMessageOutput);
QLoggingCategory mainEx("main.ex");
qCDebug(mainEx) << "debug message";
...
}
结果是从Qt后端调用下面安装的消息处理程序:
void myMessageOutput(QtMsgType type, const QMessageLogContext &context,
const QString &msg)
{
...
}
在Qt 5中,也可以直接在QML中编写调试消息:
console.debug("debug message")
但是在qmessagelogcontext中的' category '总是'qml'。是否可以在QML中直接设置另一个类别?
从Qt 5.8开始,在QML中可以开箱使用分类日志。
日志类别可以传递给console.log()和朋友第一个参数。如果提供给记录器LoggingCategory'sname将用作日志记录类别,否则为默认日志记录目录将被使用。
import QtQuick 2.8
Item {
LoggingCategory {
id: category
name: "com.qt.category"
}
Component.onCompleted: {
console.log(category, "message");
}
}
我认为没有现成的解决方案可以覆盖QML引擎中的默认类别。下面是可能的解决方案,有很好的解释和代码。