📄 debug.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Qt Toolkit - Debugging Techniques</title><style type="text/css"><!--h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }a:link { color: #004faf; text-decoration: none }a:visited { color: #672967; text-decoration: none }body { background: white; color: black; }--></style></head><body bgcolor="#ffffff"><p><table width="100%"><tr><td><a href="index.html"><img width="100" height="100" src="qtlogo.png"alt="Home" border="0"><img width="100"height="100" src="face.png" alt="Home" border="0"></a><td valign="top"><div align="right"><img src="dochead.png" width="472" height="27"><br><a href="classes.html"><b>Classes</b></a>- <a href="annotated.html">Annotated</a>- <a href="hierarchy.html">Tree</a>- <a href="functions.html">Functions</a>- <a href="index.html">Home</a>- <a href="topicals.html"><b>Structure</b> <font face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular" align="center" size=32>Qte</font></a></div></table><h1 align="center"> Debugging Techniques</h1><br clear="all">Here we present some useful hints to debugging your Qt-based software.<p><h2>Command Line Options</h2><p>When you run a Qt program you can specify several command line optionsthat can help with debugging.<p><ul><li>-nograb The application should never grab <a href="qwidget.html#5bdd5f">themouse</a> or <a href="qwidget.html#6830ee">the keyboard</a>.This option is set by default when the program is running in the <code>gdb</code> debugger under Linux.<li>-dograb Ignore any implicit or explicit -nograb. -dograb winsover -nograb even when -nograb is last on the command line.<li>-sync Runs the application in X synchronous mode. Synchronousmode forces the X server perform each X client request immediately andnot use a buffer optimization. It makes the program easier to debug andoften much slower. The -sync option is only valid for the X11version of Qt.</ul><p><hr><h2>Warning and Debugging Messages</h2><p>Qt includes three global functions for writing out warning and debugtext.<ul><li><a href="qapplication.html#72e78c">qDebug()</a> for writing debug output for testing etc.<li><a href="qapplication.html#290ef4">qWarning()</a> for writing warning output when programerrors occur.<li><a href="qapplication.html#0e1d68">qFatal()</a> for writing fatal error messages and exit.</ul><p>The Qt implementation of these functions prints the text to the <code>stderr</code>output under Unix/X11 and to the debugger under Windows. You cantake over these functions by installing a message handler;<a href="qapplication.html#a8a31b">qInstallMsgHandler()</a>.<p>The debugging functions <a href="qobject.html#c21cea">QObject::dumpObjectTree()</a> and <a href="qobject.html#d459b3">QObject::dumpObjectInfo()</a> are often useful when an application looksor acts strangely. More useful if you use object names than not, butoften useful even without names.<p><hr><h2>Debugging Macros</h2><p>The header file qglobal.h contains many debugging macros and #defines.<p>Two important macros are:<ul><li><a href="qapplication.html#6c7a8c">ASSERT(b)</a> where b is a boolean expression, writesthe warning: "ASSERT: 'b' in file file.cpp (234)" if b is FALSE.<li><a href="qapplication.html#7ed4c6">CHECK_PTR(p)</a> where p is a pointer.Writes the warning "In file file.cpp, line 234: Out of memory" if p is null.</ul><p>These macros are useful for detecting program errors, e.g. like this:<pre> char *alloc( int size ) { ASSERT( size > 0 ); char *p = new char[size]; CHECK_PTR( p ); return p; }</pre><p>If you define the flag QT_FATAL_ASSERT, ASSERT will call fatal()instead of warning(), so a failed assertion will cause the program toexit after printing the error message.<p>Note that the ASSERT macro is a null expression if <code>CHECK_STATE</code> (seebelow) is not defined. Any code in it will simply not beexecuted. Similarly CHECK_PTR is a null expression if <code>CHECK_NULL</code> isnot defined. Here is an example of how you should NOT use ASSERT andCHECK_PTR:<p><pre> char *alloc( int size ) { char *p; CHECK_PTR( p = new char[size] ); // never do this! return p; }</pre><p>The problem is tricky: <em>p</em> is set to a sane value only as long as thecorrect checking flags are defined. If this code is compiled withoutthe CHECK_NULL flag defined, the code in the CHECK_PTR expression isnot executed (correctly, since it's only a debugging aid) and <em>alloc</em>returns a wild pointer.<p>The Qt library contains hundreds of internal checks that will printwarning messages when some error is detected.<p>The tests for sanity and the resulting warning messages inside Qt areconditional, based on the state of various debugging flags:<ul><li> <code>CHECK_STATE:</code> Check for consistent/expected object state<li> <code>CHECK_RANGE:</code> Check for variables range errors<li> <code>CHECK_NULL:</code> Check for dangerous null pointer<li> <code>CHECK_MATH:</code> Check for dangerous math, e.g. division by 0.<li> <code>NO_CHECK:</code> Turn off all CHECK_... flags<li> <code>DEBUG:</code> Enable debugging code<li> <code>NO_DEBUG:</code> Turn off DEBUG flag</ul><p>By default, both DEBUG and all the CHECK flags are on. To turn offDEBUG, define NO_DEBUG. To turn off the CHECK flags, define NO_CHECK.<p>Example:<pre> void f( char *p, int i ) { #if defined(CHECK_NULL) if ( p == 0 ) <a href="qapplication.html#290ef4">qWarning</a>( "f: Null pointer not allowed" ); #endif #if defined(CHECK_RANGE) if ( i < 0 ) <a href="qapplication.html#290ef4">qWarning</a>( "f: The index cannot be negative" ); #endif }</pre><p><hr><h2>Common bugs</h2><p>There is one bug that is so common that it deserves mention here: Ifyou 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 message.<p>Any link error complaining about a lack of <code>vtbl</code>,<code>_vtbl</code>, <code>__vtbl</code> or similar is likely to bethis problem.<p><address><hr><div align="center"><table width="100%" cellspacing="0" border="0"><tr><td>Copyright
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -