Debugging TechniquesHere we present some useful hints to help you with debugging your Qt-based software. Command-Line Options Recognized by QtWhen you run a Qt application, you can specify several command-line options that can help with debugging. These are recognized by QApplication.
Warning and Debugging MessagesQt includes four global functions for writing out warning and debug text. You can use them for the following purposes:
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, and to the debugger under Windows. 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. The Object Inspector, is also an option for Qt Commercial Edition licensees. Debugging MacrosThe header file <QtGlobal> contains some debugging macros and #defines. Three important macros are:
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 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 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 OfActualités les plus luesSemaine
Mois
Année
Le blog Digia au hasardCréer des applications avec un style Metro avec Qt, exemples en QML et C++, un article de Digia Qt traduit par Thibaut CuvelierLe 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 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 4.1 | |
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