Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

How to redirect qDebug output to a file

From Wiki
Jump to: navigation, search
Article Metadata
Platform(s): Qt
Keywords: customMessageHandler,qInstallMsgHandler
Created: vasant21 (30 Aug 2010)
Last edited: hamishwillee (11 Oct 2012)

In Qt the default message handler function prints debug messages, warnings, critical and fatal error messages to standard output or to the debugger. If you wish to get more control over message handling you need to implement your own message handler and register your message handler by calling qInstallMsgHandler(yourHandlerFunction).

#include <QtDebug>
#include <QFile>
#include <QTextStream>
void customMessageHandler(QtMsgType type, const char *msg)
QString txt;
switch (type) {
case QtDebugMsg:
txt = QString("Debug: %1").arg(msg);
case QtWarningMsg:
txt = QString("Warning: %1").arg(msg);
case QtCriticalMsg:
txt = QString("Critical: %1").arg(msg);
case QtFatalMsg:
txt = QString("Fatal: %1").arg(msg);
QFile outFile("debuglog.txt"); | QIODevice::Append);
QTextStream ts(&outFile);
ts << txt << endl;
int main( int argc, char * argv[] )
QApplication app( argc, argv );
//Lets register our custom handler, before we start
return app.exec();

  • Now output of a call to qDebug() redirects to debug-log file specified in the customMessageHandler.
This page was last modified on 11 October 2012, at 01:17.
199 page views in the last 30 days.