Sophie

Sophie

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

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" />
<!-- qt4-scribe.qdoc -->
  <title>Qt 4.8: The Scribe Classes</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>The Scribe Classes</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">
  <link rel="prev" href="qt4-arthur.html" />
  <link rel="next" href="qt4-mainwindow.html" />
<p class="naviNextPrevious headerNavi">
<a class="prevPage" href="qt4-arthur.html">The Arthur Paint System</a>
<a class="nextPage" href="qt4-mainwindow.html">The Qt 4 Main Window Classes</a>
</p><p/>
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#overview-of-scribe">Overview of Scribe</a></li>
<li class="level2"><a href="#the-document-interface">The Document Interface</a></li>
<li class="level2"><a href="#document-structure">Document Structure</a></li>
<li class="level2"><a href="#editing-and-content-creation">Editing and Content Creation</a></li>
<li class="level2"><a href="#document-layout">Document Layout</a></li>
<li class="level1"><a href="#example-code">Example Code</a></li>
<li class="level2"><a href="#manipulating-rich-text">Manipulating Rich Text</a></li>
<li class="level2"><a href="#plain-text-layout">Plain Text Layout</a></li>
<li class="level2"><a href="#printing-features">Printing Features</a></li>
<li class="level1"><a href="#comparison-with-qt-3">Comparison with Qt 3</a></li>
</ul>
</div>
<h1 class="title">The Scribe Classes</h1>
<span class="subtitle"></span>
<!-- $$$qt4-scribe.html-description -->
<div class="descr"> <a name="details"></a>
<a name="scribe"></a><p>Scribe introduces a set of text layout classes to Qt 4. These classes replace the old rich text engine found in Qt 3, and provide new features for processing and laying out both plain and rich text.</p>
<p>For more details about how to use the Scribe classes, see the <a href="richtext.html">Rich Text Processing</a> document.</p>
<a name="overview-of-scribe"></a>
<h2>Overview of Scribe</h2>
<p>Support for text rendering and layout in Qt 4 has been redesigned around a system that allows textual content to be represented in a more flexible way than was possible with Qt 3. Qt 4 also provides a more convenient programming interface for editing documents. These improvements are made available through a reimplementation of the existing text rendering engine, and the introduction of several new classes.</p>
<p>The following sections provide a brief overview of the main concepts behind Scribe.</p>
<a name="the-document-interface"></a>
<h3>The Document Interface</h3>
<p>Text documents are represented by the <a href="qtextdocument.html">QTextDocument</a> class, rather than by <a href="qstring.html">QString</a> objects. Each <a href="qtextdocument.html">QTextDocument</a> object contains information about the document's internal representation, its structure, and keeps track of modifications to provide undo/redo facilities. This approach allows features such as layout management to be delegated to specialized classes, but also provides a focus for the framework.</p>
<p>Documents are either converted from external sources or created from scratch using Qt. The creation process can done by an editor widget, such as <a href="qtextedit.html">QTextEdit</a>, or by explicit calls to the Scribe API.</p>
<p>Text documents can be accessed in two complementary ways: as a linear buffer for editors to use, and as an object hierarchy that is useful to layout engines. In the hierarchical document model, objects generally correspond to visual elements such as frames, tables, and lists. At a lower level, these elements describe properties such as the text style and alignment. The linear representation of the document is used for editing and manipulation of the document's contents.</p>
<a name="document-structure"></a>
<h3>Document Structure</h3>
<p>Each document contains a root frame into which all other structural elements are placed. This frame contains other structural elements, including tables, text blocks, and other frames; these can be nested to an arbitrary depth.</p>
<p>Frames provide logical separation between parts of the document, but also have properties that determine how they will appear when rendered. A table is a specialized type of frame that consists of a number of cells, arranged into rows and columns, each of which can contain further structure and text. Tables provide management and layout features that allow flexible configurations of cells to be created.</p>
<p>Text blocks contain text fragments, each of which specifies text and character format information. Textual properties are defined both at the character level and at the block level. At the character level, properties such as font family, text color, and font weight can be specified. The block level properties control the higher level appearance and behavior of the text, such as the direction of text flow, alignment, and background color.</p>
<p>The document structure is not manipulated directly. Editing is performed through a cursor-based interface.</p>
<a name="editing-and-content-creation"></a>
<h3>Editing and Content Creation</h3>
<p>Documents can be edited via the interface provided by the <a href="qtextcursor.html">QTextCursor</a> class; cursors are either created using a constructor or obtained from an editor widget. The cursor is used to perform editing operations that correspond exactly to those the user is able to make themselves in an editor. As a result, information about the document structure is also available through the cursor, and this allows the structure to be modified. The use of a cursor-oriented interface for editing makes the process of writing a custom editor simpler for developers, since the editing operations can be easily visualized.</p>
<p>The <a href="qtextcursor.html">QTextCursor</a> class also maintains information about any text it has selected in the document, again following a model that is conceptually similar to the actions made by the user to select text in an editor.</p>
<a name="document-layout"></a>
<h3>Document Layout</h3>
<p>The layout of a document is only relevant when it is to be displayed on a device, or when some information is requested that requires a visual representation of the document. Until this occurs, the document does not need to be formatted and prepared for a device.</p>
<p>Each document's layout is managed by a subclass of the <a href="qabstracttextdocumentlayout.html">QAbstractTextDocumentLayout</a> class. This class provides a common interface for layout and rendering engines. The default rendering behavior is currently implemented in a private class. This approach makes it possible to create custom layouts, and provides the mechanism used when preparing pages for printing or exporting to Portable Document Format (PDF) files.</p>
<a name="example-code"></a>
<h2>Example Code</h2>
<p>Here we present two different ways in which the Scribe classes can be used: for creating and manipulating rich text, and for laying out plain text.</p>
<a name="manipulating-rich-text"></a>
<h3>Manipulating Rich Text</h3>
<p>Rich text is stored in text documents that can either be created by importing HTML from an external source, or generated using a <a href="qtextcursor.html">QTextCursor</a>. The easiest way to use a rich text document is through the <a href="qtextedit.html">QTextEdit</a> class, providing an editable view onto a document. The code below imports HTML into a document, and displays the document using a text edit widget.</p>
<pre class="cpp">     <span class="type"><a href="qtextedit.html">QTextEdit</a></span> <span class="operator">*</span>editor <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qtextedit.html">QTextEdit</a></span>(parent);
     editor<span class="operator">-</span><span class="operator">&gt;</span>setHtml(aStringContainingHTMLtext);
     editor<span class="operator">-</span><span class="operator">&gt;</span>show();</pre>
<p>You can retrieve the document from the text edit using the document() function. The document can then be edited programmatically using the <a href="qtextcursor.html">QTextCursor</a> class. This class is modeled after a screen cursor, and editing operations follow the same semantics. The following code changes the first line of the document to a bold font, leaving all other font properties untouched. The editor will be automatically updated to reflect the changes made to the underlying document data.</p>
<pre class="cpp">     <span class="type"><a href="qtextdocument.html">QTextDocument</a></span> <span class="operator">*</span>document <span class="operator">=</span> edit<span class="operator">-</span><span class="operator">&gt;</span>document();
     <span class="type"><a href="qtextcursor.html">QTextCursor</a></span> cursor(document);

     cursor<span class="operator">.</span>movePosition(<span class="type"><a href="qtextcursor.html">QTextCursor</a></span><span class="operator">::</span>Start);
     cursor<span class="operator">.</span>movePosition(<span class="type"><a href="qtextcursor.html">QTextCursor</a></span><span class="operator">::</span>EndOfLine<span class="operator">,</span> <span class="type"><a href="qtextcursor.html">QTextCursor</a></span><span class="operator">::</span>KeepAnchor);

     <span class="type"><a href="qtextcharformat.html">QTextCharFormat</a></span> format;
     format<span class="operator">.</span>setFontWeight(<span class="type"><a href="qfont.html">QFont</a></span><span class="operator">::</span>Bold);

     cursor<span class="operator">.</span>mergeCharFormat(format);</pre>
<p>Note that the cursor was moved from the start of the first line to the end, but that it retained an anchor at the start of the line. This demonstrates the cursor-based selection facilities of the <a href="qtextcursor.html">QTextCursor</a> class.</p>
<p>Rich text can be generated very quickly using the cursor-based approach. The following example shows a simple calendar in a <a href="qtextedit.html">QTextEdit</a> widget with bold headers for the days of the week:</p>
<pre class="cpp">     editor <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qtextedit.html">QTextEdit</a></span>(<span class="keyword">this</span>);

     <span class="type"><a href="qtextcursor.html">QTextCursor</a></span> cursor(editor<span class="operator">-</span><span class="operator">&gt;</span>textCursor());
     cursor<span class="operator">.</span>movePosition(<span class="type"><a href="qtextcursor.html">QTextCursor</a></span><span class="operator">::</span>Start);

     <span class="type"><a href="qtextcharformat.html">QTextCharFormat</a></span> format(cursor<span class="operator">.</span>charFormat());
     format<span class="operator">.</span>setFontFamily(<span class="string">&quot;Courier&quot;</span>);

     <span class="type"><a href="qtextcharformat.html">QTextCharFormat</a></span> boldFormat <span class="operator">=</span> format;
     boldFormat<span class="operator">.</span>setFontWeight(<span class="type"><a href="qfont.html">QFont</a></span><span class="operator">::</span>Bold);

     cursor<span class="operator">.</span>insertBlock();
     cursor<span class="operator">.</span>insertText(<span class="string">&quot; &quot;</span><span class="operator">,</span> boldFormat);

     <span class="type"><a href="qdate.html">QDate</a></span> date <span class="operator">=</span> <span class="type"><a href="qdate.html">QDate</a></span><span class="operator">::</span>currentDate();
     <span class="type">int</span> year <span class="operator">=</span> date<span class="operator">.</span>year()<span class="operator">,</span> month <span class="operator">=</span> date<span class="operator">.</span>month();

     <span class="keyword">for</span> (<span class="type">int</span> weekDay <span class="operator">=</span> <span class="number">1</span>; weekDay <span class="operator">&lt;</span><span class="operator">=</span> <span class="number">7</span>; <span class="operator">+</span><span class="operator">+</span>weekDay) {
         cursor<span class="operator">.</span>insertText(<span class="type"><a href="qstring.html">QString</a></span>(<span class="string">&quot;%1 &quot;</span>)<span class="operator">.</span>arg(<span class="type"><a href="qdate.html">QDate</a></span><span class="operator">::</span>shortDayName(weekDay)<span class="operator">,</span> <span class="number">3</span>)<span class="operator">,</span>
             boldFormat);
     }

     cursor<span class="operator">.</span>insertBlock();
     cursor<span class="operator">.</span>insertText(<span class="string">&quot; &quot;</span><span class="operator">,</span> format);

     <span class="keyword">for</span> (<span class="type">int</span> column <span class="operator">=</span> <span class="number">1</span>; column <span class="operator">&lt;</span> <span class="type"><a href="qdate.html">QDate</a></span>(year<span class="operator">,</span> month<span class="operator">,</span> <span class="number">1</span>)<span class="operator">.</span>dayOfWeek(); <span class="operator">+</span><span class="operator">+</span>column) {
         cursor<span class="operator">.</span>insertText(<span class="string">&quot;    &quot;</span><span class="operator">,</span> format);
     }

     <span class="keyword">for</span> (<span class="type">int</span> day <span class="operator">=</span> <span class="number">1</span>; day <span class="operator">&lt;</span><span class="operator">=</span> date<span class="operator">.</span>daysInMonth(); <span class="operator">+</span><span class="operator">+</span>day) {
         <span class="type">int</span> weekDay <span class="operator">=</span> <span class="type"><a href="qdate.html">QDate</a></span>(year<span class="operator">,</span> month<span class="operator">,</span> day)<span class="operator">.</span>dayOfWeek();

         <span class="keyword">if</span> (<span class="type"><a href="qdate.html">QDate</a></span>(year<span class="operator">,</span> month<span class="operator">,</span> day) <span class="operator">=</span><span class="operator">=</span> date)
             cursor<span class="operator">.</span>insertText(<span class="type"><a href="qstring.html">QString</a></span>(<span class="string">&quot;%1 &quot;</span>)<span class="operator">.</span>arg(day<span class="operator">,</span> <span class="number">3</span>)<span class="operator">,</span> boldFormat);
         <span class="keyword">else</span>
             cursor<span class="operator">.</span>insertText(<span class="type"><a href="qstring.html">QString</a></span>(<span class="string">&quot;%1 &quot;</span>)<span class="operator">.</span>arg(day<span class="operator">,</span> <span class="number">3</span>)<span class="operator">,</span> format);

         <span class="keyword">if</span> (weekDay <span class="operator">=</span><span class="operator">=</span> <span class="number">7</span>) {
             cursor<span class="operator">.</span>insertBlock();
             cursor<span class="operator">.</span>insertText(<span class="string">&quot; &quot;</span><span class="operator">,</span> format);
         }
     }</pre>
<p>The above example demonstrates how simple it is to quickly generate new rich text documents using a minimum amount of code. Although we have generated a crude fixed-pitch calendar to avoid quoting too much code, Scribe provides much more sophisticated layout and formatting features.</p>
<a name="plain-text-layout"></a>
<h3>Plain Text Layout</h3>
<p>Sometimes it is important to be able to format plain text within an irregularly-shaped region, perhaps when rendering a custom widget, for example. Scribe provides generic features, such as those provided by the <a href="qtextlayout.html">QTextLayout</a> class, to help developers perform word-wrapping and layout tasks without the need to create a document first.</p>
<p class="centerAlign"><img src="images/plaintext-layout.png" alt="" /></p><p>Formatting and drawing a paragraph of plain text is straightforward. The example below will lay out a paragraph of text, using a single font, around the right hand edge of a circle.</p>
<pre class="cpp">     <span class="type"><a href="qtextlayout.html">QTextLayout</a></span> textLayout(text<span class="operator">,</span> font);
     <span class="type"><a href="qtglobal.html#qreal-typedef">qreal</a></span> margin <span class="operator">=</span> <span class="number">10</span>;
     <span class="type"><a href="qtglobal.html#qreal-typedef">qreal</a></span> radius <span class="operator">=</span> <a href="qtglobal.html#qMin">qMin</a>(width()<span class="operator">/</span><span class="number">2.0</span><span class="operator">,</span> height()<span class="operator">/</span><span class="number">2.0</span>) <span class="operator">-</span> margin;
     <span class="type"><a href="qfontmetrics.html">QFontMetrics</a></span> fm(font);

     <span class="type"><a href="qtglobal.html#qreal-typedef">qreal</a></span> lineHeight <span class="operator">=</span> fm<span class="operator">.</span>height();
     <span class="type"><a href="qtglobal.html#qreal-typedef">qreal</a></span> y <span class="operator">=</span> <span class="number">0</span>;

     textLayout<span class="operator">.</span>beginLayout();

     <span class="keyword">while</span> (<span class="number">1</span>) {
         <span class="comment">// create a new line</span>
         <span class="type"><a href="qtextline.html">QTextLine</a></span> line <span class="operator">=</span> textLayout<span class="operator">.</span>createLine();
         <span class="keyword">if</span> (<span class="operator">!</span>line<span class="operator">.</span>isValid())
             <span class="keyword">break</span>;

         <span class="type"><a href="qtglobal.html#qreal-typedef">qreal</a></span> x1 <span class="operator">=</span> <a href="qtglobal.html#qMax">qMax</a>(<span class="number">0.0</span><span class="operator">,</span> pow(pow(radius<span class="operator">,</span><span class="number">2</span>)<span class="operator">-</span>pow(radius<span class="operator">-</span>y<span class="operator">,</span><span class="number">2</span>)<span class="operator">,</span> <span class="number">0.5</span>));
         <span class="type"><a href="qtglobal.html#qreal-typedef">qreal</a></span> x2 <span class="operator">=</span> <a href="qtglobal.html#qMax">qMax</a>(<span class="number">0.0</span><span class="operator">,</span> pow(pow(radius<span class="operator">,</span><span class="number">2</span>)<span class="operator">-</span>pow(radius<span class="operator">-</span>(y<span class="operator">+</span>lineHeight)<span class="operator">,</span><span class="number">2</span>)<span class="operator">,</span> <span class="number">0.5</span>));
         <span class="type"><a href="qtglobal.html#qreal-typedef">qreal</a></span> x <span class="operator">=</span> <a href="qtglobal.html#qMax">qMax</a>(x1<span class="operator">,</span> x2) <span class="operator">+</span> margin;
         <span class="type"><a href="qtglobal.html#qreal-typedef">qreal</a></span> lineWidth <span class="operator">=</span> (width() <span class="operator">-</span> margin) <span class="operator">-</span> x;

         line<span class="operator">.</span>setLineWidth(lineWidth);
         line<span class="operator">.</span>setPosition(<span class="type"><a href="qpointf.html">QPointF</a></span>(x<span class="operator">,</span> margin<span class="operator">+</span>y));
         y <span class="operator">+</span><span class="operator">=</span> line<span class="operator">.</span>height();
     }

     textLayout<span class="operator">.</span>endLayout();

     <span class="type"><a href="qpainter.html">QPainter</a></span> painter;
     painter<span class="operator">.</span>begin(<span class="keyword">this</span>);
     painter<span class="operator">.</span>setRenderHint(<span class="type"><a href="qpainter.html">QPainter</a></span><span class="operator">::</span>Antialiasing);
     painter<span class="operator">.</span>fillRect(rect()<span class="operator">,</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>white);
     painter<span class="operator">.</span>setBrush(<span class="type"><a href="qbrush.html">QBrush</a></span>(<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>black));
     painter<span class="operator">.</span>setPen(<span class="type"><a href="qpen.html">QPen</a></span>(<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>black));
     textLayout<span class="operator">.</span>draw(<span class="operator">&amp;</span>painter<span class="operator">,</span> <span class="type"><a href="qpoint.html">QPoint</a></span>(<span class="number">0</span><span class="operator">,</span><span class="number">0</span>));

     painter<span class="operator">.</span>setBrush(<span class="type"><a href="qbrush.html">QBrush</a></span>(<span class="type"><a href="qcolor.html">QColor</a></span>(<span class="string">&quot;#a6ce39&quot;</span>)));
     painter<span class="operator">.</span>setPen(<span class="type"><a href="qpen.html">QPen</a></span>(<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>black));
     painter<span class="operator">.</span>drawEllipse(<span class="type"><a href="qrectf.html">QRectF</a></span>(<span class="operator">-</span>radius<span class="operator">,</span> margin<span class="operator">,</span> <span class="number">2</span><span class="operator">*</span>radius<span class="operator">,</span> <span class="number">2</span><span class="operator">*</span>radius));
     painter<span class="operator">.</span>end();</pre>
<p>We create a text layout, specifying the text string we want to display and the font to use. We ensure that the text we supplied is formatted correctly by obtaining text lines from the text format, and wrapping the remaining text using the available space. The lines are positioned as we move down the page.</p>
<p>The formatted text can be drawn onto a paint device; in the above code, the text is drawn directly onto a widget.</p>
<a name="printing-features"></a>
<h3>Printing Features</h3>
<p>The layout system used to display rich text documents also supports paged layout of documents, and this is used by Qt to generate output for printing. The printing process is performed by <a href="qprinter.html">QPrinter</a> and controlled by the user via options displayed in a <a href="qprintdialog.html">QPrintDialog</a>:</p>
<pre class="cpp">     <span class="type"><a href="qtextdocument.html">QTextDocument</a></span> <span class="operator">*</span>document <span class="operator">=</span> editor<span class="operator">-</span><span class="operator">&gt;</span>document();
     <span class="type"><a href="qprinter.html">QPrinter</a></span> printer;

     <span class="type"><a href="qprintdialog.html">QPrintDialog</a></span> <span class="operator">*</span>dlg <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qprintdialog.html">QPrintDialog</a></span>(<span class="operator">&amp;</span>printer<span class="operator">,</span> <span class="keyword">this</span>);
     <span class="keyword">if</span> (dlg<span class="operator">-</span><span class="operator">&gt;</span>exec() <span class="operator">!</span><span class="operator">=</span> <span class="type"><a href="qdialog.html">QDialog</a></span><span class="operator">::</span>Accepted)
         <span class="keyword">return</span>;

     document<span class="operator">-</span><span class="operator">&gt;</span>print(<span class="operator">&amp;</span>printer);</pre>
<p>Rich text documents can also be exported as PDF files using <a href="qprinter.html">QPrinter</a> and the appropriate print engine:</p>
<pre class="cpp">     <span class="type"><a href="qstring.html">QString</a></span> fileName <span class="operator">=</span> <span class="type"><a href="qfiledialog.html">QFileDialog</a></span><span class="operator">::</span>getSaveFileName(<span class="keyword">this</span><span class="operator">,</span> <span class="string">&quot;Export PDF&quot;</span><span class="operator">,</span>
                                                     <span class="type"><a href="qstring.html">QString</a></span>()<span class="operator">,</span> <span class="string">&quot;*.pdf&quot;</span>);
     <span class="keyword">if</span> (<span class="operator">!</span>fileName<span class="operator">.</span>isEmpty()) {
         <span class="keyword">if</span> (<span class="type"><a href="qfileinfo.html">QFileInfo</a></span>(fileName)<span class="operator">.</span>suffix()<span class="operator">.</span>isEmpty())
             fileName<span class="operator">.</span>append(<span class="string">&quot;.pdf&quot;</span>);
         <span class="type"><a href="qprinter.html">QPrinter</a></span> printer(<span class="type"><a href="qprinter.html">QPrinter</a></span><span class="operator">::</span>HighResolution);
         printer<span class="operator">.</span>setOutputFormat(<span class="type"><a href="qprinter.html">QPrinter</a></span><span class="operator">::</span>PdfFormat);
         printer<span class="operator">.</span>setOutputFileName(fileName);
         textEdit<span class="operator">-</span><span class="operator">&gt;</span>document()<span class="operator">-</span><span class="operator">&gt;</span>print(<span class="operator">&amp;</span>printer);
     }</pre>
<a name="comparison-with-qt-3"></a>
<h2>Comparison with Qt 3</h2>
<p>The cursor-based editing features, combined with the structural document model, provide a powerful set of tools for manipulating and displaying rich text documents. These provide features that were unavailable in Qt 3's public API. The engine used is a complete rewrite and does not use the rich text engine supplied with Qt 3.</p>
<p>The <a href="qtextedit.html">QTextEdit</a> class in Qt 4 has also been completely rewritten with an API that is quite different from its Qt 3 counterpart. Some compatibility methods have been added to allow the widget to be used, for basic cases, in a way that is familiar to users of Qt 3. This class is provided as a working example of an editor widget that uses the new API, showing that it is possible to completely implement a document editor based on the <a href="qtextcursor.html">QTextCursor</a> editing interface.</p>
</div>
<!-- @@@qt4-scribe.html -->
<p class="naviNextPrevious footerNavi">
<a class="prevPage" href="qt4-arthur.html">The Arthur Paint System</a>
<a class="nextPage" href="qt4-mainwindow.html">The Qt 4 Main Window Classes</a>
</p>
      </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>