Set the QML category for console.log

I'm new to the Qt / QML topic, and I'm trying to install a logging handler in my C ++ business logic. The following code snippet sets up the handler and sets up a special category:

int main(int argc, char *argv[]) { qInstallMessageHandler(myMessageOutput); QLoggingCategory mainEx("main.ex"); qCDebug(mainEx) << "debug message"; ... } 

The result is a call from the Qt server to the following installed message handler:

 void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) { ... } 

In Qt 5, you can also write debugging messages directly in QML with:

 console.debug("debug message") 

But the "cateory" in the QMessageLogConext is always "qml". Is it possible to set another category directly in QML?

+7
c ++ qt qml
source share
2 answers

I think that there is no ready-made solution for overriding the default category in the QML engine. Here is a possible solution with very good explanation and code.

+4
source share

Starting with Qt 5.8, categorized logging is available out of the box in QML.

The log category can be passed to console.log () and friends as the first argument. If the LoggingCategory message is sent to the logger, the name will be used as the logging category, otherwise the default entry will be used.

 import QtQuick 2.8 Item { LoggingCategory { id: category name: "com.qt.category" } Component.onCompleted: { console.log(category, "message"); } } 
+6
source share

All Articles