How to redirect qml console.log () to cpp stdout

I am using qml (qtCreator) and cpp (visual studio).

Typically, error messages are displayed on the console, both from cpp and qml.

My requirement is that I should not have a console.

So, I wrote an application window.

But when the flag is set, I have to start the console. And show the corresponding error messages.

I used the following code in a function to configure it.

HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE); int hCrt = _open_osfhandle((long) handle_out, _O_TEXT); FILE* hf_out = _fdopen(hCrt, "w"); setvbuf(hf_out, NULL, _IONBF, 128); // redirecting the buffers to the file handle *stdout = *hf_out; *stderr = *hf_out; //attach std input to console HANDLE handle_in = GetStdHandle(STD_INPUT_HANDLE); hCrt = _open_osfhandle((long) handle_in, _O_TEXT); FILE* hf_in = _fdopen(hCrt, "r"); setvbuf(hf_in, NULL, _IONBF, 128); *stdin = *hf_in; 

This will print the error log from stdout and stderr to the console.

We can use qt to redirect error logs.

How to redirect qDebug, qWarning, qCritical, etc.?

But how do we redirect the output from console.log () qml to the console.

Thanks in advance.

+3
c ++ qt qt5 qml qtquick2
source share
4 answers

You can find a more detailed explanation here.

console.log just just qDebug , there is no difference in using the end. Your code should work, so I think you just haven't checked it correctly. Although your code seems weird because it doesn't necessarily depend on the platform.

You should probably get rid of this.

By the way, Qt 4 implemented something similar with a QML viewer. Here you can find the code for an example implementation, in doubt.

Hope my answer answers your question. If not, please specify.

+3
source share

If you need some kind of log from QML source code, you can create your own QML Logger object. This object will use your C ++ registration system to log in wherever you want and at your preferred level. To achieve this result, first create a C ++ class that inherits from QQuickItem , for example:

QmlLogger.hpp

 #include <QQuickItem> class QmlLogger : public QQuickItem { Q_OBJECT public: explicit QmlLogger(QQuickItem *iParent = 0); // Q_INVOKABLE log method will be called by Qml source. Q_INVOKABLE void log(unsigned int iLogLevel, const QString& iDataToLog) const; enum Level { Error = 0, Warning, Info, Debug, Trace }; Q_ENUMS(Level) private: YourLogger mYourLogger; // YourLogger is your system to log on C++ world }; 

QmlLogger.cpp

 #include <QmlLogger.hpp> // Your Constructor // Implementation of log method callable from Qml source void log(unsigned int iLogLevel, const QString& iDataToLog) const { switch(iLogLevel) { case Error: // ERROR // use you logger to log iDataToLog at error level break; case Warning: // WARNING // use you logger to log iDataToLog at warning level break; case Info: // INFO // use you logger to log iDataToLog at info level break; case Debug: // DEBUG // use you logger to log iDataToLog at debug level break; case Trace: // TRACE // use you logger to log iDataToLog at trace level break; } } 

Now you need to register a new object to make it available for the QML mechanism, then we must use the qmlRegisterType template function from the QQmlEngine class. Use this function after entering the main Qt loop, for example, as follows:

 int typeId = qmlRegisterType<QmlLogger>("QmlLogger", 1, 0, "Logger"); // if typeId is 0 => Error Q_ASSERT(typeId); 

In C ++, we are done. Now in the QML source we can use the new object in such a simple way.

 import QmlLogger 1.0 Logger{ id: logger } function aFunctionThatYouWantToDebug(iArgumentOne, iArgumentTwo){ // logging logger.log(Logger.Debug, "Entering function aFunctionThatYouWantToDebug(" + iArgumentOne + ", " + iArgumentTwo + ")") // body of function ... } 

Calling the log method in the QML source is equivalent to calling the log method in the C ++ QmlLogger , which writes to your log file, data is logged.

+4
source share

Use QMLLogging . It is fast, typical and multi-functional.

Disclosure: I am the author of this library

+2
source share

This is the same process as for other Qt posts. The message sent to console.log() will reach the Qt message handler installed with the severity of QtDebugMsg.

+1
source share

All Articles