Debugging TechniquesHere we present some useful hints to debugging your Qt-based software.
Command Line OptionsWhen you run a Qt program you can specify several command line options that can help with debugging.
Warning and Debugging MessagesQt includes three global functions for writing out warning and debug text.
The Qt implementation of these functions prints the text to the stderr output under Unix/X11 and to the debugger under Windows. You can take over these functions by installing a message handler; qInstallMsgHandler(). 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.
Debugging MacrosThe header file qglobal.h contains many debugging macros and #defines. Two important macros are:
These macros are useful for detecting program errors, e.g. like this: char *alloc( int size ) { Q_ASSERT( size > 0 ); char *p = new char[size]; Q_CHECK_PTR( p ); return p; } If you define the flag QT_FATAL_ASSERT, Q_ASSERT will call fatal() instead of warning(), so a failed assertion will cause the program to exit after printing the error message. Note that the Q_ASSERT macro is a null expression if QT_CHECK_STATE (see below) is not defined. Any code in it will simply not be executed. Similarly Q_CHECK_PTR is a null expression if QT_CHECK_NULL is not defined. Here is an example of how you should not use Q_ASSERT and Q_CHECK_PTR:
char *alloc( int size ) { char *p; Q_CHECK_PTR( p = new char[size] ); // WRONG! return p; } The problem is tricky: p is set to a sane value only as long as the correct checking flags are defined. If this code is compiled without the QT_CHECK_NULL flag defined, the code in the Q_CHECK_PTR expression is not executed (correctly, since it's only a debugging aid) and alloc returns a wild pointer. The Qt library contains hundreds of internal checks that will print warning messages when some error is detected. The tests for sanity and the resulting warning messages inside Qt are conditional, based on the state of various debugging flags:
By default, both QT_DEBUG and all the QT_CHECK flags are on. To turn off QT_DEBUG, define QT_NO_DEBUG. To turn off the QT_CHECK flags, define QT_NO_CHECK. Example: void f( char *p, int i ) { #if defined(QT_CHECK_NULL) if ( p == 0 ) qWarning( "f: Null pointer not allowed" ); #endif #if defined(QT_CHECK_RANGE) if ( i < 0 ) qWarning( "f: The index cannot be negative" ); #endif }
Common bugsThere 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 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 OfActualités les plus luesSemaine
Mois
Année
Le Qt Labs au hasardLe moteur de rendu OpenGLLes Qt Labs sont les laboratoires des développeurs de Qt, où ils peuvent partager des impressions sur le framework, son utilisation, ce que pourrait être son futur. Lire l'article.
CommunautéRessources
Liens utilesContact
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 3.3 | |
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 ! |
Copyright © 2000-2012 - www.developpez.com