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, ...

Get Programming with Qt, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.