Is there any way to tell the application to print only qDebug
or only qFatal
messages?
Asked
Active
Viewed 1,158 times
1 Answers
2
Yes, there is. You can install your own message handler with qInstallMessageHandler
.
This looks something like that (from the Qt docs):
void myMessageOutput(QtMsgType type, const QMessageLogContext &context,
const QString &msg)
{
QByteArray localMsg = msg.toLocal8Bit();
switch (type) {
case QtDebugMsg:
fprintf(stderr, "Debug: %s (%s:%u, %s)\n", localMsg.constData(),
context.file, context.line, context.function);
break;
case QtWarningMsg:
fprintf(stderr, "Warning: %s (%s:%u, %s)\n", localMsg.constData(),
context.file, context.line, context.function);
break;
case QtCriticalMsg:
fprintf(stderr, "Critical: %s (%s:%u, %s)\n", localMsg.constData(),
context.file, context.line, context.function);
break;
case QtFatalMsg:
fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(),
context.file, context.line, context.function);
abort();
}
}
int main(int argc, char **argv)
{
qInstallMessageHandler(myMessageOutput);
QApplication app(argc, argv);
...
return app.exec();
}
Gives you 100% control of how qDebug and the other q** output functions work.

Greenflow
- 3,935
- 2
- 17
- 28