August 2018
Intermediate to advanced
534 pages
11h 21m
English
Developers commonly need to have logs. In some situations, you will not have access to the console output, or you will have to study the application state afterwards. In both cases, the log has to be output to a file.
Qt provides a practical way of redirecting your logs (qDebug, qInfo, qWarning, and so on) to any device that is convenient for you: QtMessageHandler. To use it, you have to register a function that will save the logs to the desired output.
For example, in your main.cpp, add the following function:
#include <QFile> #include <QTextStream> void messageHander(QtMsgType type, const QMessageLogContext& context, const QString& message) { QString levelText; switch (type) { case QtDebugMsg: levelText = "Debug"; ...Read now
Unlock full access