Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Toutes les fonctions  ·  Vues d'ensemble  · 

Debugging Techniques

Here we present some useful hints to help you with debugging your Qt-based software.

Configuring Qt for Debugging

When configuring Qt for installation, it is possible to ensure that it is built to include debug symbols that can make it easier to track bugs in applications and libraries. However, on some platforms, building Qt in debug mode will cause applications to be larger than desirable.

Debugging in Mac OS X and Xcode

Debugging With/Without Frameworks

The basic stuff you need to know about debug libraries and frameworks is found at developer.apple.com in: Apple Technicle Note TN2124 Qt follows that.

When you build Qt, frameworks are built by default, and inside the framework you will find both a release and a debug version (e.g., QtCore and QtCore_debug). If you pass the -no-framework flag when you build Qt, two dylibs are built for each Qt library (e.g., libQtCore.4.dylib and libQtCore_debug.4.dylib).

What happens when you link depends on whether you use frameworks or not. We don't see a compelling reason to recommend one over the other.

With Frameworks:

Since the release and debug libraries are inside the framework, the app is simply linked against the framework. Then when you run in the debugger, you will get either the release version or the debug version, depending on whether you set DYLD_IMAGE_SUFFIX. If you don't set it, you get the release version by default (i.e., non _debug). If you set DYLD_IMAGE_SUFFIX=_debug, you get the debug version.

Without Frameworks:

When you tell qmake to generate a Makefile with the debug config, it will link against the _debug version of the libraries and generate debug symbols for the app. Running this program in GDB will then work like running GDB on other platforms, and you will be able to trace inside Qt.

Debug Symbols and Size

The amount of space taken up by debug symbols generated by GCC can be excessively large. However, with the release of Xcode 2.3 it is now possible to use Dwarf symbols which take up a significantly smaller amount of space. To enable this feature when configuring Qt, pass the -dwarf-2 option to the configure script.

This is not enabled by default because previous versions of Xcode will not work with the compiler flag used to implement this feature. Mac OS X 10.5 will use dwarf-2 symbols by default.

dwarf-2 symbols contain references to source code, so the size of the final debug application should compare favorably to a release build.

Command Line Options Recognized by Qt

When you run a Qt application, you can specify several command-line options that can help with debugging. These are recognized by QApplication.

OptionDescription
-nograbThe application should never grab the mouse or the keyboard. This option is set by default when the program is running in the gdb debugger under Linux.
-dograbIgnore any implicit or explicit -nograb. -dograb wins over -nograb even when -nograb is last on the command line.
-syncRuns the application in X synchronous mode. Synchronous mode forces the X server to perform each X client request immediately and not use buffer optimization. It makes the program easier to debug and often much slower. The -sync option is only valid for the X11 version of Qt.

Warning and Debugging Messages

Qt includes four global functions for writing out warning and debug text. You can use them for the following purposes:

  • qDebug() is used for writing custom debug output.
  • qWarning() is used to report warnings and recoverable errors in your application.
  • qCritical() is used for writing critical error mesages and reporting system errors.
  • qFatal() is used for writing fatal error messages shortly before exiting.

If you include the <QtDebug> header file, the qDebug() function can also be used as an output stream. For example:

 qDebug() << "Widget" << widget << "at position" << widget->pos();

The Qt implementation of these functions prints the text to the stderr output under Unix/X11 and Mac OS X. With Windows, if it is a console application, the text is sent to console; otherwise, it is sent to the debugger. You can take over these functions by installing a message handler using qInstallMsgHandler().

If the QT_FATAL_WARNINGS environment variable is set, qWarning() exits after printing the warning message. This makes it easy to obtain a backtrace in the debugger.

Both qDebug() and qWarning() are debugging tools. They can be compiled away by defining QT_NO_DEBUG_OUTPUT and QT_NO_WARNING_OUTPUT during compilation.

The debugging functions QObject::dumpObjectTree() and QObject::dumpObjectInfo() are often useful when an application looks or acts strangely. More useful if you use object names than not, but often useful even without names.

Providing Support for the qDebug() Stream Operator

You can implement the stream operator used by qDebug() to provide debugging support for your classes. The class that implements the stream is QDebug. The functions you need to know about in QDebug are space() and nospace(). They both return a debug stream; the difference between them is whether a space is inserted between each item. Here is an example for a class that represents a 2D coordinate.

 QDebug operator<<(QDebug dbg, const Coordinate &c)
 {
     dbg.nospace() << "(" << c.x() << ", " << c.y() << ")";

     return dbg.space();
 }

Integration of custom types with Qt's meta-object system is covered in more depth in the Creating Custom Qt Types document.

Debugging Macros

The header file <QtGlobal> contains some debugging macros and #defines.

Three important macros are:

  • Q_ASSERT(cond), where cond is a boolean expression, writes the warning "ASSERT: 'cond' in file xyz.cpp, line 234" and exits if cond is false.
  • Q_ASSERT_X(cond, where, what), where cond is a boolean expression, where a location, and what a message, writes the warning: "ASSERT failure in where: 'what', file xyz.cpp, line 234" and exits if cond is false.
  • Q_CHECK_PTR(ptr), where ptr is a pointer. Writes the warning "In file xyz.cpp, line 234: Out of memory" and exits if ptr is 0.

These macros are useful for detecting program errors, e.g. like this:

 char *alloc(int size)
 {
     Q_ASSERT(size > 0);
     char *ptr = new char[size];
     Q_CHECK_PTR(ptr);
     return ptr;
 }

Q_ASSERT(), Q_ASSERT_X(), and Q_CHECK_PTR() expand to nothing if QT_NO_DEBUG is defined during compilation. For this reason, the arguments to these macro should not have any side-effects. Here is an incorrect usage of Q_CHECK_PTR():

 char *alloc(int size)
 {
     char *ptr;
     Q_CHECK_PTR(ptr = new char[size]);  // WRONG
     return ptr;
 }

If this code is compiled with QT_NO_DEBUG defined, the code in the Q_CHECK_PTR() expression is not executed and alloc returns an unitialized pointer.

The Qt library contains hundreds of internal checks that will print warning messages when a programming error is detected. We therefore recommend that you use a debug version of Qt when developing Qt-based software.

Common Bugs

There is one bug that is so common that it deserves mention here: If you include the Q_OBJECT macro in a class declaration and run the meta-object compiler (moc), but forget to link the moc-generated object code into your executable, you will get very confusing error messages. Any link error complaining about a lack of vtbl, _vtbl, __vtbl or similar is likely to be a result of this problem.

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 53
  2. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  3. BlackBerry 10 : premières images du prochain OS de RIM qui devrait intégrer des widgets et des tuiles inspirées de Windows Phone 0
  4. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  5. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  6. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
  7. La rubrique Qt a besoin de vous ! 1
Page suivante

Le blog Digia au hasard

Logo

Déploiement d'applications Qt Commercial sur les tablettes Windows 8

Le blog Digia est l'endroit privilégié pour la communication sur l'édition commerciale de Qt, où des réponses publiques sont apportées aux questions les plus posées au support. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 4.6
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web