Sophie

Sophie

distrib > Fedora > 18 > x86_64 > by-pkgid > ff187cb994c94c614ecc64c5a8528b1b > files > 179

qt-doc-4.8.5-10.fc18.noarch.rpm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- debug.qdoc -->
  <title>Qt 4.8: Debugging Techniques</title>
  <link rel="stylesheet" type="text/css" href="style/style.css" />
  <script src="scripts/jquery.js" type="text/javascript"></script>
  <script src="scripts/functions.js" type="text/javascript"></script>
  <link rel="stylesheet" type="text/css" href="style/superfish.css" />
  <link rel="stylesheet" type="text/css" href="style/narrow.css" />
  <!--[if IE]>
<meta name="MSSmartTagsPreventParsing" content="true">
<meta http-equiv="imagetoolbar" content="no">
<![endif]-->
<!--[if lt IE 7]>
<link rel="stylesheet" type="text/css" href="style/style_ie6.css">
<![endif]-->
<!--[if IE 7]>
<link rel="stylesheet" type="text/css" href="style/style_ie7.css">
<![endif]-->
<!--[if IE 8]>
<link rel="stylesheet" type="text/css" href="style/style_ie8.css">
<![endif]-->

<script src="scripts/superfish.js" type="text/javascript"></script>
<script src="scripts/narrow.js" type="text/javascript"></script>

</head>
<body class="" onload="CheckEmptyAndLoadList();">
 <div class="header" id="qtdocheader">
    <div class="content"> 
    <div id="nav-logo">
      <a href="index.html">Home</a></div>
    <a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
    <div id="narrowsearch"></div>
    <div id="nav-topright">
      <ul>
        <li class="nav-topright-home"><a href="http://qt.digia.com/">Qt HOME</a></li>
        <li class="nav-topright-dev"><a href="http://qt-project.org/">DEV</a></li>
        <li class="nav-topright-doc nav-topright-doc-active"><a href="http://qt-project.org/doc/">
          DOC</a></li>
        <li class="nav-topright-blog"><a href="http://blog.qt.digia.com/">BLOG</a></li>
      </ul>
    </div>
    <div id="shortCut">
      <ul>
        <li class="shortCut-topleft-inactive"><span><a href="index.html">Qt 4.8</a></span></li>
        <li class="shortCut-topleft-active"><a href="http://qt-project.org/doc/">ALL VERSIONS        </a></li>
      </ul>
     </div>
 <ul class="sf-menu" id="narrowmenu"> 
             <li><a href="#">API Lookup</a> 
                 <ul> 
                     <li><a href="classes.html">Class index</a></li> 
           <li><a href="functions.html">Function index</a></li> 
           <li><a href="modules.html">Modules</a></li> 
           <li><a href="namespaces.html">Namespaces</a></li> 
           <li><a href="qtglobal.html">Global Declarations</a></li> 
           <li><a href="qdeclarativeelements.html">QML elements</a></li> 
             </ul> 
             </li> 
             <li><a href="#">Qt Topics</a> 
                 <ul> 
                        <li><a href="qt-basic-concepts.html">Programming with Qt</a></li>  
                        <li><a href="qtquick.html">Device UIs &amp; Qt Quick</a></li>  
                        <li><a href="qt-gui-concepts.html">UI Design with Qt</a></li>  
                        <li><a href="supported-platforms.html">Supported Platforms</a></li>  
                        <li><a href="technology-apis.html">Qt and Key Technologies</a></li>  
                        <li><a href="best-practices.html">How-To's and Best Practices</a></li>  
              </ul> 
                 </li> 
                 <li><a href="#">Examples</a> 
                     <ul> 
                       <li><a href="all-examples.html">Examples</a></li> 
                       <li><a href="tutorials.html">Tutorials</a></li> 
                       <li><a href="demos.html">Demos</a></li> 
                       <li><a href="qdeclarativeexamples.html">QML Examples</a></li> 
                </ul> 
                     </li> 
                 </ul> 
    </div>
  </div>
  <div class="wrapper">
    <div class="hd">
      <span></span>
    </div>
    <div class="bd group">
      <div class="sidebar">
        <div class="searchlabel">
          Search index:</div>
        <div class="search" id="sidebarsearch">
          <form id="qtdocsearch" action="" onsubmit="return false;">
            <fieldset>
              <input type="text" name="searchstring" id="pageType" value="" />
 <div id="resultdialog"> 
 <a href="#" id="resultclose">Close</a> 
 <p id="resultlinks" class="all"><a href="#" id="showallresults">All</a> | <a href="#" id="showapiresults">API</a> | <a href="#" id="showarticleresults">Articles</a> | <a href="#" id="showexampleresults">Examples</a></p> 
 <p id="searchcount" class="all"><span id="resultcount"></span><span id="apicount"></span><span id="articlecount"></span><span id="examplecount"></span>&nbsp;results:</p> 
 <ul id="resultlist" class="all"> 
 </ul> 
 </div> 
            </fieldset>
          </form>
        </div>
        <div class="box first bottombar" id="lookup">
          <h2 title="API Lookup"><span></span>
            API Lookup</h2>
          <div  id="list001" class="list">
          <ul id="ul001" >
              <li class="defaultLink"><a href="classes.html">Class index</a></li>
              <li class="defaultLink"><a href="functions.html">Function index</a></li>
              <li class="defaultLink"><a href="modules.html">Modules</a></li>
              <li class="defaultLink"><a href="namespaces.html">Namespaces</a></li>
              <li class="defaultLink"><a href="qtglobal.html">Global Declarations</a></li>
              <li class="defaultLink"><a href="qdeclarativeelements.html">QML elements</a></li>
            </ul> 
          </div>
        </div>
        <div class="box bottombar" id="topics">
          <h2 title="Qt Topics"><span></span>
            Qt Topics</h2>
          <div id="list002" class="list">
            <ul id="ul002" >
               <li class="defaultLink"><a href="qt-basic-concepts.html">Programming with Qt</a></li> 
               <li class="defaultLink"><a href="qtquick.html">Device UIs &amp; Qt Quick</a></li> 
               <li class="defaultLink"><a href="qt-gui-concepts.html">UI Design with Qt</a></li> 
               <li class="defaultLink"><a href="supported-platforms.html">Supported Platforms</a></li>  
               <li class="defaultLink"><a href="technology-apis.html">Qt and Key Technologies</a></li> 
               <li class="defaultLink"><a href="best-practices.html">How-To's and Best Practices</a></li> 
            </ul>  
          </div>
        </div>
        <div class="box" id="examples">
          <h2 title="Examples"><span></span>
            Examples</h2>
          <div id="list003" class="list">
        <ul id="ul003">
              <li class="defaultLink"><a href="all-examples.html">Examples</a></li>
              <li class="defaultLink"><a href="tutorials.html">Tutorials</a></li>
              <li class="defaultLink"><a href="demos.html">Demos</a></li>
              <li class="defaultLink"><a href="qdeclarativeexamples.html">QML Examples</a></li>
            </ul> 
          </div>
        </div>
      </div>
      <div class="wrap">
        <div class="toolbar">
          <div class="breadcrumb toolblock">
            <ul>
              <li class="first"><a href="index.html">Home</a></li>
              <!--  Breadcrumbs go here -->
<li>Debugging Techniques</li>
            </ul>
          </div>
          <div class="toolbuttons toolblock">
            <ul>
              <li id="smallA" class="t_button">A</li>
              <li id="medA" class="t_button active">A</li>
              <li id="bigA" class="t_button">A</li>
              <li id="print" class="t_button"><a href="javascript:this.print();">
                <span>Print</span></a></li>
            </ul>
        </div>
        </div>
        <div class="content mainContent">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#configuring-qt-for-debugging">Configuring Qt for Debugging</a></li>
<li class="level2"><a href="#debugging-in-mac-os-x-and-xcode">Debugging in Mac OS X and Xcode</a></li>
<li class="level3"><a href="#debugging-with-without-frameworks">Debugging With/Without Frameworks</a></li>
<li class="level4"><a href="#with-frameworks">With Frameworks:</a></li>
<li class="level4"><a href="#without-frameworks">Without Frameworks:</a></li>
<li class="level3"><a href="#debug-symbols-and-size">Debug Symbols and Size</a></li>
<li class="level1"><a href="#command-line-options-recognized-by-qt">Command Line Options Recognized by Qt</a></li>
<li class="level1"><a href="#warning-and-debugging-messages">Warning and Debugging Messages</a></li>
<li class="level1"><a href="#providing-support-for-the-qdebug-stream-operator">Providing Support for the qDebug() Stream Operator</a></li>
<li class="level1"><a href="#debugging-macros">Debugging Macros</a></li>
<li class="level1"><a href="#common-bugs">Common Bugs</a></li>
</ul>
</div>
<h1 class="title">Debugging Techniques</h1>
<span class="subtitle"></span>
<!-- $$$debug.html-description -->
<div class="descr"> <a name="details"></a>
<p>Here we present some useful hints to help you with debugging your Qt-based software.</p>
<a name="configuring-qt-for-debugging"></a>
<h2>Configuring Qt for Debugging</h2>
<p>When <a href="installation.html">configuring Qt for installation</a>, 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.</p>
<a name="debugging-in-mac-os-x-and-xcode"></a>
<h3>Debugging in Mac OS X and Xcode</h3>
<a name="debugging-with-without-frameworks"></a>
<h4>Debugging With/Without Frameworks</h4>
<p>The basic stuff you need to know about debug libraries and frameworks is found at developer.apple.com in: <a href="http://developer.apple.com/technotes/tn2004/tn2124.html#SECDEBUGLIB">Apple Technical Note TN2124</a>.</p>
<p>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&#x2e;, <a href="qtcore.html">QtCore</a> and QtCore_debug). If you pass the <tt>-no-framework</tt> flag when you build Qt, two dylibs are built for each Qt library (e.g&#x2e;, libQtCore.4.dylib and libQtCore_debug.4.dylib).</p>
<p>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.</p>
<a name="with-frameworks"></a>
<h5>With Frameworks:</h5>
<p>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 <tt>DYLD_IMAGE_SUFFIX</tt>. If you don't set it, you get the release version by default (i.e&#x2e;, non _debug). If you set <tt>DYLD_IMAGE_SUFFIX=_debug</tt>, you get the debug version.</p>
<a name="without-frameworks"></a>
<h5>Without Frameworks:</h5>
<p>When you tell <i>qmake</i> 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.</p>
<a name="debug-symbols-and-size"></a>
<h4>Debug Symbols and Size</h4>
<p>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 <tt>-dwarf2</tt> option to the configure script.</p>
<p>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.</p>
<p>dwarf-2 symbols contain references to source code, so the size of the final debug application should compare favorably to a release build.</p>
<a name="command-line-options-recognized-by-qt"></a>
<h2>Command Line Options Recognized by Qt</h2>
<p>When you run a Qt application, you can specify several command-line options that can help with debugging. These are recognized by <a href="qapplication.html">QApplication</a>.</p>
<table class="generic">
<thead><tr class="qt-style"><th >Option</th><th >Description</th></tr></thead>
<tr valign="top" class="odd"><td ><tt>-nograb</tt></td><td >The application should never grab <a href="qwidget.html#grabMouse">the mouse</a> or <a href="qwidget.html#grabKeyboard">the keyboard</a>. This option is set by default when the program is running in the <tt>gdb</tt> debugger under Linux.</td></tr>
<tr valign="top" class="even"><td ><tt>-dograb</tt></td><td >Ignore any implicit or explicit <tt>-nograb</tt>. <tt>-dograb</tt> wins over <tt>-nograb</tt> even when <tt>-nograb</tt> is last on the command line.</td></tr>
<tr valign="top" class="odd"><td ><tt>-sync</tt></td><td >Runs 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 <tt>-sync</tt> option is only valid for the X11 version of Qt.</td></tr>
</table>
<a name="warning-and-debugging-messages"></a>
<h2>Warning and Debugging Messages</h2>
<p>Qt includes four global functions for writing out warning and debug text. You can use them for the following purposes:</p>
<ul>
<li><a href="qtglobal.html#qDebug">qDebug</a>() is used for writing custom debug output.</li>
<li><a href="qtglobal.html#qWarning">qWarning</a>() is used to report warnings and recoverable errors in your application.</li>
<li><a href="qtglobal.html#qCritical">qCritical</a>() is used for writing critical error mesages and reporting system errors.</li>
<li><a href="qtglobal.html#qFatal">qFatal</a>() is used for writing fatal error messages shortly before exiting.</li>
</ul>
<p>If you include the &lt;QtDebug&gt; header file, the <tt>qDebug()</tt> function can also be used as an output stream. For example:</p>
<pre class="cpp"> <a href="qtglobal.html#qDebug">qDebug</a>() <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;Widget&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> widget <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;at position&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> widget<span class="operator">-</span><span class="operator">&gt;</span>pos();</pre>
<p>The Qt implementation of these functions prints the text to the <tt>stderr</tt> 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 <a href="qtglobal.html#qInstallMsgHandler">qInstallMsgHandler</a>().</p>
<p>If the <tt>QT_FATAL_WARNINGS</tt> environment variable is set, <a href="qtglobal.html#qWarning">qWarning</a>() exits after printing the warning message. This makes it easy to obtain a backtrace in the debugger.</p>
<p>Both <a href="qtglobal.html#qDebug">qDebug</a>() and <a href="qtglobal.html#qWarning">qWarning</a>() are debugging tools. They can be compiled away by defining <tt>QT_NO_DEBUG_OUTPUT</tt> and <tt>QT_NO_WARNING_OUTPUT</tt> during compilation.</p>
<p>The debugging functions <a href="qobject.html#dumpObjectTree">QObject::dumpObjectTree</a>() and <a href="qobject.html#dumpObjectInfo">QObject::dumpObjectInfo</a>() are often useful when an application looks or acts strangely. More useful if you use <a href="qobject.html#objectName-prop">object names</a> than not, but often useful even without names.</p>
<a name="providing-support-for-the-qdebug-stream-operator"></a>
<h2>Providing Support for the qDebug() Stream Operator</h2>
<p>You can implement the stream operator used by <a href="qtglobal.html#qDebug">qDebug</a>() to provide debugging support for your classes. The class that implements the stream is <tt>QDebug</tt>. The functions you need to know about in <tt>QDebug</tt> are <tt>space()</tt> and <tt>nospace()</tt>. 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.</p>
<pre class="cpp"> <span class="type"><a href="qdebug.html">QDebug</a></span> <span class="keyword">operator</span><span class="operator">&lt;</span><span class="operator">&lt;</span>(<span class="type"><a href="qdebug.html">QDebug</a></span> dbg<span class="operator">,</span> <span class="keyword">const</span> Coordinate <span class="operator">&amp;</span>c)
 {
     dbg<span class="operator">.</span>nospace() <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;(&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> c<span class="operator">.</span>x() <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;, &quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> c<span class="operator">.</span>y() <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;)&quot;</span>;

     <span class="keyword">return</span> dbg<span class="operator">.</span>space();
 }</pre>
<p>Integration of custom types with Qt's meta-object system is covered in more depth in the <a href="custom-types.html">Creating Custom Qt Types</a> document.</p>
<a name="debugging-macros"></a>
<h2>Debugging Macros</h2>
<p>The header file <tt>&lt;QtGlobal&gt;</tt> contains some debugging macros and <tt>#define</tt>s.</p>
<p>Three important macros are:</p>
<ul>
<li><a href="qtglobal.html#Q_ASSERT">Q_ASSERT</a>(cond), where <tt>cond</tt> is a boolean expression, writes the warning &quot;ASSERT: '<i>cond</i>' in file xyz.cpp, line 234&quot; and exits if <tt>cond</tt> is false.</li>
<li><a href="qtglobal.html#Q_ASSERT_X">Q_ASSERT_X</a>(cond, where, what), where <tt>cond</tt> is a boolean expression, <tt>where</tt> a location, and <tt>what</tt> a message, writes the warning: &quot;ASSERT failure in <tt>where</tt>: '<tt>what</tt>', file xyz.cpp, line 234&quot; and exits if <tt>cond</tt> is false.</li>
<li><a href="qtglobal.html#Q_CHECK_PTR">Q_CHECK_PTR</a>(ptr), where <tt>ptr</tt> is a pointer. Writes the warning &quot;In file xyz.cpp, line 234: Out of memory&quot; and exits if <tt>ptr</tt> is 0.</li>
</ul>
<p>These macros are useful for detecting program errors, e.g&#x2e; like this:</p>
<pre class="cpp"> <span class="type">char</span> <span class="operator">*</span>alloc(<span class="type">int</span> size)
 {
     Q_ASSERT(size <span class="operator">&gt;</span> <span class="number">0</span>);
     <span class="type">char</span> <span class="operator">*</span>ptr <span class="operator">=</span> <span class="keyword">new</span> <span class="type">char</span><span class="operator">[</span>size<span class="operator">]</span>;
     Q_CHECK_PTR(ptr);
     <span class="keyword">return</span> ptr;
 }</pre>
<p><a href="qtglobal.html#Q_ASSERT">Q_ASSERT</a>(), <a href="qtglobal.html#Q_ASSERT_X">Q_ASSERT_X</a>(), and <a href="qtglobal.html#Q_CHECK_PTR">Q_CHECK_PTR</a>() expand to nothing if <tt>QT_NO_DEBUG</tt> is defined during compilation. For this reason, the arguments to these macro should not have any side-effects. Here is an incorrect usage of <a href="qtglobal.html#Q_CHECK_PTR">Q_CHECK_PTR</a>():</p>
<pre class="cpp"> <span class="type">char</span> <span class="operator">*</span>alloc(<span class="type">int</span> size)
 {
     <span class="type">char</span> <span class="operator">*</span>ptr;
     Q_CHECK_PTR(ptr <span class="operator">=</span> <span class="keyword">new</span> <span class="type">char</span><span class="operator">[</span>size<span class="operator">]</span>);  <span class="comment">// WRONG</span>
     <span class="keyword">return</span> ptr;
 }</pre>
<p>If this code is compiled with <tt>QT_NO_DEBUG</tt> defined, the code in the <a href="qtglobal.html#Q_CHECK_PTR">Q_CHECK_PTR</a>() expression is not executed and <i>alloc</i> returns an unitialized pointer.</p>
<p>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.</p>
<a name="common-bugs"></a>
<h2>Common Bugs</h2>
<p>There is one bug that is so common that it deserves mention here: If you include the <a href="qobject.html#Q_OBJECT">Q_OBJECT</a> macro in a class declaration and run <a href="moc.html">the meta-object compiler</a> (<tt>moc</tt>), but forget to link the <tt>moc</tt>-generated object code into your executable, you will get very confusing error messages. Any link error complaining about a lack of <tt>vtbl</tt>, <tt>_vtbl</tt>, <tt>__vtbl</tt> or similar is likely to be a result of this problem.</p>
</div>
<!-- @@@debug.html -->
      </div>
    </div>
    </div> 
    <div class="ft">
      <span></span>
    </div>
  </div> 
  <div class="footer">
    <p>
      <acronym title="Copyright">&copy;</acronym> 2013 Digia Plc and/or its
      subsidiaries. Documentation contributions included herein are the copyrights of
      their respective owners.</p>
    <br />
    <p>
      The documentation provided herein is licensed under the terms of the
      <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation
      License version 1.3</a> as published by the Free Software Foundation.</p>
    <p>
      Documentation sources may be obtained from <a href="http://www.qt-project.org">
      www.qt-project.org</a>.</p>
    <br />
    <p>
      Digia, Qt and their respective logos are trademarks of Digia Plc 
      in Finland and/or other countries worldwide. All other trademarks are property
      of their respective owners. <a title="Privacy Policy"
      href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p>
  </div>

  <script src="scripts/functions.js" type="text/javascript"></script>
</body>
</html>