The debugging technology of QT Core programming (g)

Source: Internet
Author: User
Tags gdb debugger

The QT application is debugged to debug and print various debug or warning messages via DDD. DDD (Data Display Debugger) is a graphical tool that uses the GDB debugging tool, which is installed in the Linux operating system and is used to refer to the Help documentation for DDD.

Ad:51cto The first China app Innovation contest is recruiting >>

QT Core programming Debugging Technology is the content of this sectionto introduction,QT Core programming we want to be divided into several parts to introduce, want to refer to more content, please see the editor at the end of the recommendation for detailed reading, first look at this article.

The Qt application is debugged to debug and print various debug or warning messages via DDD. DDD (Data Display Debugger) is a graphical tool that uses the GDB debugging tool, which is installed in the Linux operating system and is used to refer to the Help documentation for DDD. Here's how to print a variety of debug or warning messages

1. Command line Parameters

When you run the Q application, you can specify several command-line parameters to help you debug. These command-line arguments are described below:

The-nograb application no longer captures the mouse or keyboard. This option is default when the program is running under Linux in the GDB debugger.

-dograb ignores any implied or obvious-nograb. Even if-nograb appears at the end of the command line,-dograb will be more than-nograb effective.

-sync running the application in X-sync mode. Synchronous mode forces the X server to immediately execute requests for each x client without using cache optimizations. It makes the program easier to test and usually slower. -sync mode is only valid for X11 versions of QT.

2. Print warnings and debug messages

Qt uses three global functions Qdebug, qwarning, and qfatal to print warning and debug information to the standard error output stderr (which, by default, is a display or file). These three functions are described below:

Qdebug () is used to print debug information, output information in debug builds, and the function will not work in the release version.

Qwarning () is used to print a warning message when a program error occurs.

Qfatal () is used to print the fatal error message and exits.

The Qt implementation of these functions prints the text to the standard error output (stderr) under unix/x11 and prints it to the debugger under Windows. You can receive these functions by installing a message handler, Qinstallmsghandler ().

Since the implementation of these 3 functions is similar, here only the parse function Qdebug,qdebug function's parameter format is similar to the function printf, printing the formatted string. The Qdebug function is listed below (in Src/tools/qglobal.cpp):

  1. Static Qtmsghandler handler = 0; A handle to a user-defined print output function
  2. static const int qt_buffer_length = 8196;//Internal BUFFER length
  3. void Qdebug (const char *msg, ...)//msg formatted string that needs to be printed
  4. {
  5. Char Buf[qt_buffer_length];
  6. Va_list ap;
  7. Va_start (AP, msg);//Use variable list of parameters
  8. #if defined (qt_vsnprintf)
  9. qt_vsnprintf (buf, Qt_buffer_length, MSG, AP);
  10. #else vsprintf (BUF, MSG, AP); Put the information you need to print into BUF
  11. #endif Va_end (AP);
  12. if (handler) {//If a user-specified output function exists, use it to output information
  13. (*handler) (Qtdebugmsg, BUF);
  14. } else {
  15. #if defined (q_cc_mwerks)
  16. Mac_default_handler (BUF); Default output function under Mac system
  17. #elif defined (q_os_temp)
  18. QString fstr (BUF);
  19. OutputDebugString ((fstr + "\ n"). UCS2 ());
  20. #else fprintf (stderr, "%s\n", buf); Output to Stderr#endif
  21. }
  22. }

The function type of Qtmsghandler is defined in src/tools/qglobal.h and the function qinstallmsghandler is defined to output the function name from the dynamic library. These two definitions are listed below:

typedef void (*qtmsghandler) (qtmsgtype, const char *);//Q_export represents the output of this function name Q_export Qtmsghandler Qinstallmsghandler in a dynamic library (Qtmsghandler);

The function Qinstallmsghandler is used by the user to define an installation handler function and returns a pointer to a previously defined message handler function. Only one message handler function can be defined in an application. When you restore the previous message handler function, call Qinstallmsghandler (0). The functions are listed below (in Src/tools/qglobal.cpp):

    1. Qtmsghandler Qinstallmsghandler (Qtmsghandler h) {
    2. Qtmsghandler old = handler;
    3. hhandler = h;
    4. return old;
    5. }

Example: Applying Qinstallmsghandler

The following example shows how to install your own program run information output function in an application. This example first defines the information output function Mymessageoutput, and then installs the information output function in the main function of the program. When this application function is run, the function mymessageoutput is used to output the run information. The code is as follows:

  1. #include <qapplication.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. void Mymessageoutput (Qtmsgtype type, const char *msg)//define information output function {
  5. Switch (type) {
  6. Case QTDEBUGMSG://Output Debug information
  7. fprintf (stderr, "Debug:%s\n", msg);
  8. Break
  9. Case QTWARNINGMSG://Output warning message
  10. fprintf (stderr, "Warning:%s\n", msg);
  11. Break
  12. Case QTFATALMSG://Output Fatal message
  13. fprintf (stderr, "Fatal:%s\n", msg);
  14. Abort (); Interrupt operation, exit program
  15. }
  16. }
  17. int main (int argc, char **argv) {
  18. Qinstallmsghandler (Mymessageoutput); Installation Information output function
  19. Qapplication A (argc, argv);
  20. ...
  21. return A.exec ();
  22. }

There are two other debugging functions for printing object information qobject::d umpobjecttree () and Qobject::d umpobjectinfo (). They only output information in the debug version of the program, and in the release version, these two functions do not work. function Qobject::d umpobjectinfo () prints information about an object's signal connection. function Qobject::d umpobjecttree () prints out the sub-object tree.

3. Debug macros

Macros Q_assert and q_check_ptr are often used to output information in a program run, as described in the following two macros:

(1) b in Q_assert (b) is a Boolean expression, and when B is false, a similar warning message is printed: "ASSERT: ' B ' in file File.cpp (234)".

(2) p in Q_check_ptr (p) is a pointer. If P is empty, a similar warning message is printed: "In file File.cpp, line 234:out of memory".

Macro Q_assert is essentially the call function qfatal or qwarning output information, listed below (in src/tools/qglobal.h):

    1. #if!defined (Q_assert)
    2. # if defined (qt_check_state)
    3. # if defined (Qt_fatal_assert)
    4. # define Q_ASSERT (x)//print x, file name, line number in program source code
    5. # Else
    6. # define Q_ASSERT (x)
    7. # endif
    8. # Else
    9. # define Q_ASSERT (x)
    10. # ENDIF#ENDIF

Macro Q_check_ptr essentially calls the function qwarning output information, and the macro definition q_check_ptr is listed below (in src/tools/qglobal.h):

    1. #if defined (qt_check_null)
    2. # define Q_CHECK_PTR (p) (qt_check_pointer#else# define Q_CHECK_PTR (p)
    3. #endif q_export BOOL Qt_check_pointer (bool c, const char *, int);

The function Qt_check_pointer implements the information output operation, which is listed as follows (in src/tools/qglobal.cpp):

    1. BOOL Qt_check_pointer (bool C, const char *n, int l) {if (c) qwarning ("in file%s, line%d:out of Memory", N, L);
    2. return TRUE;}

Example 2: Run Macros Q_assert and Q_assert

Macro Q_assert and Q_assert are commonly used to detect program errors, and the following examples use these two macros:

    1. char *alloc (int size) {
    2. Q_assert (Size > 0);//If the size > 0 expression is not valid, print a warning message
    3. char *p = new char[size];
    4. Q_check_ptr (P); If the pointer p is empty, print a warning message
    5. return p;
    6. }

Qt prints different types of warning messages based on different debug tags. Qt uses the following macro definitions to illustrate the different debug tags (in src/tools/qglobal.h):

Qt_check_state: Detecting consistent/expected object state

Qt_check_range: Detection Variable range error

Qt_check_null: Detecting a dangerous null pointer

Qt_check_math: Detecting dangerous mathematics, such as being removed by 0

Qt_no_check: Close all Qt_check_ ... Mark

Qt_debug: Making Debugging code effective

Qt_no_debug: Close Qt_debug Tag

By default, Qt_debug and all Qt_check tags are turned on. If you want to turn off Qt_debug, define Qt_no_debug. If you want to turn off the Qt_check tag, define Qt_no_check.

Example 3: Print different types of warning messages

The following example prints different types of warning messages based on different macro definitions. The code is as follows:

    1. void f (char *p, int i) {
    2. #if defined (qt_check_null)//detection of dangerous null pointers
    3. if ( p = = 0) qwarning ("F:null pointer not allowed");
    4. #endif #if defined (qt_check_range)//detection variable range error
    5. if (I < 0)
    6. Qwarning ("F:the index cannot be negative");
    7. #endif}

Summary:QT core programming Debugging Technology of the content is finished, need this article can help you, need more information, please refer to the editorial recommendation.

Debugging technology for QT Core programming (g)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.