O'Reilly logo

Programming with Qt, 2nd Edition by Matthias Kalle Dalheimer

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 21. Debugging

This chapter does not cover general debugging techniques—there are already plenty of good books about that topic. Instead, this chapter lists features that Qt provides specifically to help with debugging.

First, Qt provides the functions qDebug(), qWarning(), and qFatal(), which are all declared in qglobal.h. These functions work just like printf() in that they accept a format string and a variable number of parameters. The advantage of these functions is that they work equally well with Unix or Windows. In Unix, the output is written to standard error. In Windows, it is sent to the debugger. In addition to printing the message, qFatal() exits the program. All three functions restrict the text output to no more than 8,196 characters, including the trailing zero byte.

If you want to redirect the output from these functions, you can install your own message handler. This is especially interesting because Qt contains hundreds of warning messages. Most warn the developer about invalid function parameters. Even though you should take all efforts to avoid provoking these warnings, you might want to rule out any possibility that your users will see them. A custom message handler can solve this problem.

Such a message handler is installed with the function qInstallMsgHandler(). As its only argument, it expects the address of a function—the message handler itself—with the following signature:

void (*)( QtMsgType type, const char *msg );

The second parameter is the message, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required