Sophie

Sophie

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

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" />
<!-- qsyntaxhighlighter.cpp -->
  <title>Qt 4.8: QSyntaxHighlighter Class Reference</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><a href="modules.html">Modules</a></li>
<li><a href="qtgui.html">QtGui</a></li>
<li>QSyntaxHighlighter</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="#public-functions">Public Functions</a></li>
<li class="level1"><a href="#public-slots">Public Slots</a></li>
<li class="level1"><a href="#protected-functions">Protected Functions</a></li>
<li class="level1"><a href="#details">Detailed Description</a></li>
</ul>
</div>
<h1 class="title">QSyntaxHighlighter Class Reference</h1>
<!-- $$$QSyntaxHighlighter-brief -->
<p>The QSyntaxHighlighter class allows you to define syntax highlighting rules, and in addition you can use the class to query a document's current formatting or user data. <a href="#details">More...</a></p>
<!-- @@@QSyntaxHighlighter -->
<pre class="cpp"> <span class="preprocessor">#include &lt;QSyntaxHighlighter&gt;</span></pre><p><b>Inherits: </b><a href="qobject.html">QObject</a>.</p>
<p><b>Note:</b> All functions in this class are <a href="threads-reentrancy.html#reentrant">reentrant</a>.</p>
<p>This class was introduced in Qt 4.1.</p>
<ul>
<li><a href="qsyntaxhighlighter-members.html">List of all members, including inherited members</a></li>
</ul>
<a name="public-functions"></a>
<h2>Public Functions</h2>
<table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qsyntaxhighlighter.html#QSyntaxHighlighter">QSyntaxHighlighter</a></b> ( QObject * <i>parent</i> )</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qsyntaxhighlighter.html#QSyntaxHighlighter-2">QSyntaxHighlighter</a></b> ( QTextDocument * <i>parent</i> )</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qsyntaxhighlighter.html#QSyntaxHighlighter-3">QSyntaxHighlighter</a></b> ( QTextEdit * <i>parent</i> )</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> virtual </td><td class="memItemRight bottomAlign"><b><a href="qsyntaxhighlighter.html#dtor.QSyntaxHighlighter">~QSyntaxHighlighter</a></b> ()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QTextDocument * </td><td class="memItemRight bottomAlign"><b><a href="qsyntaxhighlighter.html#document">document</a></b> () const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qsyntaxhighlighter.html#setDocument">setDocument</a></b> ( QTextDocument * <i>doc</i> )</td></tr>
</table>
<ul>
<li class="fn">29 public functions inherited from <a href="qobject.html#public-functions">QObject</a></li>
</ul>
<a name="public-slots"></a>
<h2>Public Slots</h2>
<table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qsyntaxhighlighter.html#rehighlight">rehighlight</a></b> ()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qsyntaxhighlighter.html#rehighlightBlock">rehighlightBlock</a></b> ( const QTextBlock &amp; <i>block</i> )</td></tr>
</table>
<ul>
<li class="fn">1 public slot inherited from <a href="qobject.html#public-slots">QObject</a></li>
</ul>
<a name="protected-functions"></a>
<h2>Protected Functions</h2>
<table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> QTextBlock </td><td class="memItemRight bottomAlign"><b><a href="qsyntaxhighlighter.html#currentBlock">currentBlock</a></b> () const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> int </td><td class="memItemRight bottomAlign"><b><a href="qsyntaxhighlighter.html#currentBlockState">currentBlockState</a></b> () const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QTextBlockUserData * </td><td class="memItemRight bottomAlign"><b><a href="qsyntaxhighlighter.html#currentBlockUserData">currentBlockUserData</a></b> () const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QTextCharFormat </td><td class="memItemRight bottomAlign"><b><a href="qsyntaxhighlighter.html#format">format</a></b> ( int <i>position</i> ) const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> virtual void </td><td class="memItemRight bottomAlign"><b><a href="qsyntaxhighlighter.html#highlightBlock">highlightBlock</a></b> ( const QString &amp; <i>text</i> ) = 0</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> int </td><td class="memItemRight bottomAlign"><b><a href="qsyntaxhighlighter.html#previousBlockState">previousBlockState</a></b> () const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qsyntaxhighlighter.html#setCurrentBlockState">setCurrentBlockState</a></b> ( int <i>newState</i> )</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qsyntaxhighlighter.html#setCurrentBlockUserData">setCurrentBlockUserData</a></b> ( QTextBlockUserData * <i>data</i> )</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qsyntaxhighlighter.html#setFormat">setFormat</a></b> ( int <i>start</i>, int <i>count</i>, const QTextCharFormat &amp; <i>format</i> )</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qsyntaxhighlighter.html#setFormat-2">setFormat</a></b> ( int <i>start</i>, int <i>count</i>, const QColor &amp; <i>color</i> )</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qsyntaxhighlighter.html#setFormat-3">setFormat</a></b> ( int <i>start</i>, int <i>count</i>, const QFont &amp; <i>font</i> )</td></tr>
</table>
<ul>
<li class="fn">8 protected functions inherited from <a href="qobject.html#protected-functions">QObject</a></li>
</ul>
<h3>Additional Inherited Members</h3>
<ul>
<li class="fn">1 property inherited from <a href="qobject.html#properties">QObject</a></li>
<li class="fn">1 signal inherited from <a href="qobject.html#signals">QObject</a></li>
<li class="fn">7 static public members inherited from <a href="qobject.html#static-public-members">QObject</a></li>
</ul>
<a name="details"></a>
<!-- $$$QSyntaxHighlighter-description -->
<div class="descr">
<h2>Detailed Description</h2>
<p>The QSyntaxHighlighter class allows you to define syntax highlighting rules, and in addition you can use the class to query a document's current formatting or user data.</p>
<p>The QSyntaxHighlighter class is a base class for implementing <a href="qtextedit.html">QTextEdit</a> syntax highlighters. A syntax highligher automatically highlights parts of the text in a <a href="qtextedit.html">QTextEdit</a>, or more generally in a <a href="qtextdocument.html">QTextDocument</a>. Syntax highlighters are often used when the user is entering text in a specific format (for example source code) and help the user to read the text and identify syntax errors.</p>
<p>To provide your own syntax highlighting, you must subclass QSyntaxHighlighter and reimplement <a href="qsyntaxhighlighter.html#highlightBlock">highlightBlock</a>().</p>
<p>When you create an instance of your QSyntaxHighlighter subclass, pass it the <a href="qtextedit.html">QTextEdit</a> or <a href="qtextdocument.html">QTextDocument</a> that you want the syntax highlighting to be applied to. For example:</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>;
 MyHighlighter <span class="operator">*</span>highlighter <span class="operator">=</span> <span class="keyword">new</span> MyHighlighter(editor<span class="operator">-</span><span class="operator">&gt;</span>document());</pre>
<p>After this your <a href="qsyntaxhighlighter.html#highlightBlock">highlightBlock</a>() function will be called automatically whenever necessary. Use your <a href="qsyntaxhighlighter.html#highlightBlock">highlightBlock</a>() function to apply formatting (e.g&#x2e; setting the font and color) to the text that is passed to it. QSyntaxHighlighter provides the <a href="qsyntaxhighlighter.html#setFormat">setFormat</a>() function which applies a given <a href="qtextcharformat.html">QTextCharFormat</a> on the current text block. For example:</p>
<pre class="cpp"> <span class="type">void</span> MyHighlighter<span class="operator">::</span><a href="qsyntaxhighlighter.html#highlightBlock">highlightBlock</a>(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&amp;</span>text)
 {
     <span class="type"><a href="qtextcharformat.html">QTextCharFormat</a></span> myClassFormat;
     myClassFormat<span class="operator">.</span>setFontWeight(<span class="type"><a href="qfont.html">QFont</a></span><span class="operator">::</span>Bold);
     myClassFormat<span class="operator">.</span>setForeground(<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>darkMagenta);
     <span class="type"><a href="qstring.html">QString</a></span> pattern <span class="operator">=</span> <span class="string">&quot;\\bMy[A-Za-z]+\\b&quot;</span>;

     <span class="type"><a href="qregexp.html">QRegExp</a></span> expression(pattern);
     <span class="type">int</span> index <span class="operator">=</span> text<span class="operator">.</span>indexOf(expression);
     <span class="keyword">while</span> (index <span class="operator">&gt;</span><span class="operator">=</span> <span class="number">0</span>) {
         <span class="type">int</span> length <span class="operator">=</span> expression<span class="operator">.</span>matchedLength();
         setFormat(index<span class="operator">,</span> length<span class="operator">,</span> myClassFormat);
         index <span class="operator">=</span> text<span class="operator">.</span>indexOf(expression<span class="operator">,</span> index <span class="operator">+</span> length);
     }
 }</pre>
<p>Some syntaxes can have constructs that span several text blocks. For example, a C++ syntax highlighter should be able to cope with <tt>/</tt><tt>*..&#x2e;*</tt><tt>/</tt> multiline comments. To deal with these cases it is necessary to know the end state of the previous text block (e.g&#x2e; &quot;in comment&quot;).</p>
<p>Inside your <a href="qsyntaxhighlighter.html#highlightBlock">highlightBlock</a>() implementation you can query the end state of the previous text block using the <a href="qsyntaxhighlighter.html#previousBlockState">previousBlockState</a>() function. After parsing the block you can save the last state using <a href="qsyntaxhighlighter.html#setCurrentBlockState">setCurrentBlockState</a>().</p>
<p>The <a href="qsyntaxhighlighter.html#currentBlockState">currentBlockState</a>() and <a href="qsyntaxhighlighter.html#previousBlockState">previousBlockState</a>() functions return an int value. If no state is set, the returned value is -1. You can designate any other value to identify any given state using the <a href="qsyntaxhighlighter.html#setCurrentBlockState">setCurrentBlockState</a>() function. Once the state is set the <a href="qtextblock.html">QTextBlock</a> keeps that value until it is set set again or until the corresponding paragraph of text is deleted.</p>
<p>For example, if you're writing a simple C++ syntax highlighter, you might designate 1 to signify &quot;in comment&quot;:</p>
<pre class="cpp"> <span class="type"><a href="qtextcharformat.html">QTextCharFormat</a></span> multiLineCommentFormat;
 multiLineCommentFormat<span class="operator">.</span>setForeground(<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>red);

 <span class="type"><a href="qregexp.html">QRegExp</a></span> startExpression(<span class="string">&quot;/\\*&quot;</span>);
 <span class="type"><a href="qregexp.html">QRegExp</a></span> endExpression(<span class="string">&quot;\\*/&quot;</span>);

 <a href="qsyntaxhighlighter.html#setCurrentBlockState">setCurrentBlockState</a>(<span class="number">0</span>);

 <span class="type">int</span> startIndex <span class="operator">=</span> <span class="number">0</span>;
 <span class="keyword">if</span> (previousBlockState() <span class="operator">!</span><span class="operator">=</span> <span class="number">1</span>)
     startIndex <span class="operator">=</span> text<span class="operator">.</span>indexOf(startExpression);

 <span class="keyword">while</span> (startIndex <span class="operator">&gt;</span><span class="operator">=</span> <span class="number">0</span>) {
    <span class="type">int</span> endIndex <span class="operator">=</span> text<span class="operator">.</span>indexOf(endExpression<span class="operator">,</span> startIndex);
    <span class="type">int</span> commentLength;
    <span class="keyword">if</span> (endIndex <span class="operator">=</span><span class="operator">=</span> <span class="operator">-</span><span class="number">1</span>) {
        setCurrentBlockState(<span class="number">1</span>);
        commentLength <span class="operator">=</span> text<span class="operator">.</span>length() <span class="operator">-</span> startIndex;
    } <span class="keyword">else</span> {
        commentLength <span class="operator">=</span> endIndex <span class="operator">-</span> startIndex
                        <span class="operator">+</span> endExpression<span class="operator">.</span>matchedLength();
    }
    setFormat(startIndex<span class="operator">,</span> commentLength<span class="operator">,</span> multiLineCommentFormat);
    startIndex <span class="operator">=</span> text<span class="operator">.</span>indexOf(startExpression<span class="operator">,</span>
                              startIndex <span class="operator">+</span> commentLength);
 }</pre>
<p>In the example above, we first set the current block state to 0. Then, if the previous block ended within a comment, we higlight from the beginning of the current block (<tt>startIndex = 0</tt>). Otherwise, we search for the given start expression. If the specified end expression cannot be found in the text block, we change the current block state by calling <a href="qsyntaxhighlighter.html#setCurrentBlockState">setCurrentBlockState</a>(), and make sure that the rest of the block is higlighted.</p>
<p>In addition you can query the current formatting and user data using the <a href="qsyntaxhighlighter.html#format">format</a>() and <a href="qsyntaxhighlighter.html#currentBlockUserData">currentBlockUserData</a>() functions respectively. You can also attach user data to the current text block using the <a href="qsyntaxhighlighter.html#setCurrentBlockUserData">setCurrentBlockUserData</a>() function. <a href="qtextblockuserdata.html">QTextBlockUserData</a> can be used to store custom settings. In the case of syntax highlighting, it is in particular interesting as cache storage for information that you may figure out while parsing the paragraph's text. For an example, see the <a href="qsyntaxhighlighter.html#setCurrentBlockUserData">setCurrentBlockUserData</a>() documentation.</p>
</div>
<p><b>See also </b><a href="qtextedit.html">QTextEdit</a> and <a href="richtext-syntaxhighlighter.html">Syntax Highlighter Example</a>.</p>
<!-- @@@QSyntaxHighlighter -->
<div class="func">
<h2>Member Function Documentation</h2>
<!-- $$$QSyntaxHighlighter[overload1]$$$QSyntaxHighlighterQObject* -->
<h3 class="fn"><a name="QSyntaxHighlighter"></a>QSyntaxHighlighter::<span class="name">QSyntaxHighlighter</span> ( <span class="type"><a href="qobject.html">QObject</a></span> * <i>parent</i> )</h3>
<p>Constructs a <a href="qsyntaxhighlighter.html">QSyntaxHighlighter</a> with the given <i>parent</i>.</p>
<!-- @@@QSyntaxHighlighter -->
<!-- $$$QSyntaxHighlighter$$$QSyntaxHighlighterQTextDocument* -->
<h3 class="fn"><a name="QSyntaxHighlighter-2"></a>QSyntaxHighlighter::<span class="name">QSyntaxHighlighter</span> ( <span class="type"><a href="qtextdocument.html">QTextDocument</a></span> * <i>parent</i> )</h3>
<p>Constructs a <a href="qsyntaxhighlighter.html">QSyntaxHighlighter</a> and installs it on <i>parent</i>. The specified <a href="qtextdocument.html">QTextDocument</a> also becomes the owner of the <a href="qsyntaxhighlighter.html">QSyntaxHighlighter</a>.</p>
<!-- @@@QSyntaxHighlighter -->
<!-- $$$QSyntaxHighlighter$$$QSyntaxHighlighterQTextEdit* -->
<h3 class="fn"><a name="QSyntaxHighlighter-3"></a>QSyntaxHighlighter::<span class="name">QSyntaxHighlighter</span> ( <span class="type"><a href="qtextedit.html">QTextEdit</a></span> * <i>parent</i> )</h3>
<p>Constructs a <a href="qsyntaxhighlighter.html">QSyntaxHighlighter</a> and installs it on <i>parent</i> 's <a href="qtextdocument.html">QTextDocument</a>. The specified <a href="qtextedit.html">QTextEdit</a> also becomes the owner of the <a href="qsyntaxhighlighter.html">QSyntaxHighlighter</a>.</p>
<!-- @@@QSyntaxHighlighter -->
<!-- $$$~QSyntaxHighlighter[overload1]$$$~QSyntaxHighlighter -->
<h3 class="fn"><a name="dtor.QSyntaxHighlighter"></a>QSyntaxHighlighter::<span class="name">~QSyntaxHighlighter</span> ()<tt> [virtual]</tt></h3>
<p>Destructor. Uninstalls this syntax highlighter from the text document.</p>
<!-- @@@~QSyntaxHighlighter -->
<!-- $$$currentBlock[overload1]$$$currentBlock -->
<h3 class="fn"><a name="currentBlock"></a><span class="type"><a href="qtextblock.html">QTextBlock</a></span> QSyntaxHighlighter::<span class="name">currentBlock</span> () const<tt> [protected]</tt></h3>
<p>Returns the current text block.</p>
<p>This function was introduced in Qt 4.4.</p>
<!-- @@@currentBlock -->
<!-- $$$currentBlockState[overload1]$$$currentBlockState -->
<h3 class="fn"><a name="currentBlockState"></a><span class="type">int</span> QSyntaxHighlighter::<span class="name">currentBlockState</span> () const<tt> [protected]</tt></h3>
<p>Returns the state of the current text block. If no value is set, the returned value is -1.</p>
<p><b>See also </b><a href="qsyntaxhighlighter.html#setCurrentBlockState">setCurrentBlockState</a>().</p>
<!-- @@@currentBlockState -->
<!-- $$$currentBlockUserData[overload1]$$$currentBlockUserData -->
<h3 class="fn"><a name="currentBlockUserData"></a><span class="type"><a href="qtextblockuserdata.html">QTextBlockUserData</a></span> * QSyntaxHighlighter::<span class="name">currentBlockUserData</span> () const<tt> [protected]</tt></h3>
<p>Returns the <a href="qtextblockuserdata.html">QTextBlockUserData</a> object previously attached to the current text block.</p>
<p><b>See also </b><a href="qtextblock.html#userData">QTextBlock::userData</a>() and <a href="qsyntaxhighlighter.html#setCurrentBlockUserData">setCurrentBlockUserData</a>().</p>
<!-- @@@currentBlockUserData -->
<!-- $$$document[overload1]$$$document -->
<h3 class="fn"><a name="document"></a><span class="type"><a href="qtextdocument.html">QTextDocument</a></span> * QSyntaxHighlighter::<span class="name">document</span> () const</h3>
<p>Returns the <a href="qtextdocument.html">QTextDocument</a> on which this syntax highlighter is installed.</p>
<p><b>See also </b><a href="qsyntaxhighlighter.html#setDocument">setDocument</a>().</p>
<!-- @@@document -->
<!-- $$$format[overload1]$$$formatint -->
<h3 class="fn"><a name="format"></a><span class="type"><a href="qtextcharformat.html">QTextCharFormat</a></span> QSyntaxHighlighter::<span class="name">format</span> ( <span class="type">int</span> <i>position</i> ) const<tt> [protected]</tt></h3>
<p>Returns the format at <i>position</i> inside the syntax highlighter's current text block.</p>
<p><b>See also </b><a href="qsyntaxhighlighter.html#setFormat">setFormat</a>().</p>
<!-- @@@format -->
<!-- $$$highlightBlock[overload1]$$$highlightBlockconstQString& -->
<h3 class="fn"><a name="highlightBlock"></a><span class="type">void</span> QSyntaxHighlighter::<span class="name">highlightBlock</span> ( const <span class="type"><a href="qstring.html">QString</a></span> &amp; <i>text</i> )<tt> [pure virtual protected]</tt></h3>
<p>Highlights the given text block. This function is called when necessary by the rich text engine, i.e&#x2e; on text blocks which have changed.</p>
<p>To provide your own syntax highlighting, you must subclass <a href="qsyntaxhighlighter.html">QSyntaxHighlighter</a> and reimplement highlightBlock(). In your reimplementation you should parse the block's <i>text</i> and call <a href="qsyntaxhighlighter.html#setFormat">setFormat</a>() as often as necessary to apply any font and color changes that you require. For example:</p>
<pre class="cpp"> <span class="type">void</span> MyHighlighter<span class="operator">::</span>highlightBlock(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&amp;</span>text)
 {
     <span class="type"><a href="qtextcharformat.html">QTextCharFormat</a></span> myClassFormat;
     myClassFormat<span class="operator">.</span>setFontWeight(<span class="type"><a href="qfont.html">QFont</a></span><span class="operator">::</span>Bold);
     myClassFormat<span class="operator">.</span>setForeground(<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>darkMagenta);
     <span class="type"><a href="qstring.html">QString</a></span> pattern <span class="operator">=</span> <span class="string">&quot;\\bMy[A-Za-z]+\\b&quot;</span>;

     <span class="type"><a href="qregexp.html">QRegExp</a></span> expression(pattern);
     <span class="type">int</span> index <span class="operator">=</span> text<span class="operator">.</span>indexOf(expression);
     <span class="keyword">while</span> (index <span class="operator">&gt;</span><span class="operator">=</span> <span class="number">0</span>) {
         <span class="type">int</span> length <span class="operator">=</span> expression<span class="operator">.</span>matchedLength();
         setFormat(index<span class="operator">,</span> length<span class="operator">,</span> myClassFormat);
         index <span class="operator">=</span> text<span class="operator">.</span>indexOf(expression<span class="operator">,</span> index <span class="operator">+</span> length);
      }
  }</pre>
<p>Some syntaxes can have constructs that span several text blocks. For example, a C++ syntax highlighter should be able to cope with <tt>/</tt><tt>*..&#x2e;*</tt><tt>/</tt> multiline comments. To deal with these cases it is necessary to know the end state of the previous text block (e.g&#x2e; &quot;in comment&quot;).</p>
<p>Inside your highlightBlock() implementation you can query the end state of the previous text block using the <a href="qsyntaxhighlighter.html#previousBlockState">previousBlockState</a>() function. After parsing the block you can save the last state using <a href="qsyntaxhighlighter.html#setCurrentBlockState">setCurrentBlockState</a>().</p>
<p>The <a href="qsyntaxhighlighter.html#currentBlockState">currentBlockState</a>() and <a href="qsyntaxhighlighter.html#previousBlockState">previousBlockState</a>() functions return an int value. If no state is set, the returned value is -1. You can designate any other value to identify any given state using the <a href="qsyntaxhighlighter.html#setCurrentBlockState">setCurrentBlockState</a>() function. Once the state is set the <a href="qtextblock.html">QTextBlock</a> keeps that value until it is set set again or until the corresponding paragraph of text gets deleted.</p>
<p>For example, if you're writing a simple C++ syntax highlighter, you might designate 1 to signify &quot;in comment&quot;. For a text block that ended in the middle of a comment you'd set 1 using setCurrentBlockState, and for other paragraphs you'd set 0. In your parsing code if the return value of <a href="qsyntaxhighlighter.html#previousBlockState">previousBlockState</a>() is 1, you would highlight the text as a C++ comment until you reached the closing <tt>*</tt><tt>/</tt>.</p>
<p><b>See also </b><a href="qsyntaxhighlighter.html#previousBlockState">previousBlockState</a>(), <a href="qsyntaxhighlighter.html#setFormat">setFormat</a>(), and <a href="qsyntaxhighlighter.html#setCurrentBlockState">setCurrentBlockState</a>().</p>
<!-- @@@highlightBlock -->
<!-- $$$previousBlockState[overload1]$$$previousBlockState -->
<h3 class="fn"><a name="previousBlockState"></a><span class="type">int</span> QSyntaxHighlighter::<span class="name">previousBlockState</span> () const<tt> [protected]</tt></h3>
<p>Returns the end state of the text block previous to the syntax highlighter's current block. If no value was previously set, the returned value is -1.</p>
<p><b>See also </b><a href="qsyntaxhighlighter.html#highlightBlock">highlightBlock</a>() and <a href="qsyntaxhighlighter.html#setCurrentBlockState">setCurrentBlockState</a>().</p>
<!-- @@@previousBlockState -->
<!-- $$$rehighlight[overload1]$$$rehighlight -->
<h3 class="fn"><a name="rehighlight"></a><span class="type">void</span> QSyntaxHighlighter::<span class="name">rehighlight</span> ()<tt> [slot]</tt></h3>
<p>Reapplies the highlighting to the whole document.</p>
<p>This function was introduced in Qt 4.2.</p>
<p><b>See also </b><a href="qsyntaxhighlighter.html#rehighlightBlock">rehighlightBlock</a>().</p>
<!-- @@@rehighlight -->
<!-- $$$rehighlightBlock[overload1]$$$rehighlightBlockconstQTextBlock& -->
<h3 class="fn"><a name="rehighlightBlock"></a><span class="type">void</span> QSyntaxHighlighter::<span class="name">rehighlightBlock</span> ( const <span class="type"><a href="qtextblock.html">QTextBlock</a></span> &amp; <i>block</i> )<tt> [slot]</tt></h3>
<p>Reapplies the highlighting to the given <a href="qtextblock.html">QTextBlock</a> <i>block</i>.</p>
<p>This function was introduced in Qt 4.6.</p>
<p><b>See also </b><a href="qsyntaxhighlighter.html#rehighlight">rehighlight</a>().</p>
<!-- @@@rehighlightBlock -->
<!-- $$$setCurrentBlockState[overload1]$$$setCurrentBlockStateint -->
<h3 class="fn"><a name="setCurrentBlockState"></a><span class="type">void</span> QSyntaxHighlighter::<span class="name">setCurrentBlockState</span> ( <span class="type">int</span> <i>newState</i> )<tt> [protected]</tt></h3>
<p>Sets the state of the current text block to <i>newState</i>.</p>
<p><b>See also </b><a href="qsyntaxhighlighter.html#currentBlockState">currentBlockState</a>() and <a href="qsyntaxhighlighter.html#highlightBlock">highlightBlock</a>().</p>
<!-- @@@setCurrentBlockState -->
<!-- $$$setCurrentBlockUserData[overload1]$$$setCurrentBlockUserDataQTextBlockUserData* -->
<h3 class="fn"><a name="setCurrentBlockUserData"></a><span class="type">void</span> QSyntaxHighlighter::<span class="name">setCurrentBlockUserData</span> ( <span class="type"><a href="qtextblockuserdata.html">QTextBlockUserData</a></span> * <i>data</i> )<tt> [protected]</tt></h3>
<p>Attaches the given <i>data</i> to the current text block. The ownership is passed to the underlying text document, i.e&#x2e; the provided <a href="qtextblockuserdata.html">QTextBlockUserData</a> object will be deleted if the corresponding text block gets deleted.</p>
<p><a href="qtextblockuserdata.html">QTextBlockUserData</a> can be used to store custom settings. In the case of syntax highlighting, it is in particular interesting as cache storage for information that you may figure out while parsing the paragraph's text.</p>
<p>For example while parsing the text, you can keep track of parenthesis characters that you encounter ('{[(' and the like), and store their relative position and the actual <a href="qchar.html">QChar</a> in a simple class derived from <a href="qtextblockuserdata.html">QTextBlockUserData</a>:</p>
<pre class="cpp"> <span class="keyword">struct</span> ParenthesisInfo
 {
     <span class="type"><a href="qchar.html">QChar</a></span> <span class="type">char</span>;
     <span class="type">int</span> position;
 };

 <span class="keyword">struct</span> BlockData : <span class="keyword">public</span> <span class="type"><a href="qtextblockuserdata.html">QTextBlockUserData</a></span>
 {
     <span class="type"><a href="qvector.html">QVector</a></span><span class="operator">&lt;</span>ParenthesisInfo<span class="operator">&gt;</span> parentheses;
 };</pre>
<p>During cursor navigation in the associated editor, you can ask the current <a href="qtextblock.html">QTextBlock</a> (retrieved using the <a href="qtextcursor.html#block">QTextCursor::block</a>() function) if it has a user data object set and cast it to your <tt>BlockData</tt> object. Then you can check if the current cursor position matches with a previously recorded parenthesis position, and, depending on the type of parenthesis (opening or closing), find the next opening or closing parenthesis on the same level.</p>
<p>In this way you can do a visual parenthesis matching and highlight from the current cursor position to the matching parenthesis. That makes it easier to spot a missing parenthesis in your code and to find where a corresponding opening/closing parenthesis is when editing parenthesis intensive code.</p>
<p><b>See also </b><a href="qsyntaxhighlighter.html#currentBlockUserData">currentBlockUserData</a>() and <a href="qtextblock.html#setUserData">QTextBlock::setUserData</a>().</p>
<!-- @@@setCurrentBlockUserData -->
<!-- $$$setDocument[overload1]$$$setDocumentQTextDocument* -->
<h3 class="fn"><a name="setDocument"></a><span class="type">void</span> QSyntaxHighlighter::<span class="name">setDocument</span> ( <span class="type"><a href="qtextdocument.html">QTextDocument</a></span> * <i>doc</i> )</h3>
<p>Installs the syntax highlighter on the given <a href="qtextdocument.html">QTextDocument</a> <i>doc</i>. A <a href="qsyntaxhighlighter.html">QSyntaxHighlighter</a> can only be used with one document at a time.</p>
<p><b>See also </b><a href="qsyntaxhighlighter.html#document">document</a>().</p>
<!-- @@@setDocument -->
<!-- $$$setFormat[overload1]$$$setFormatintintconstQTextCharFormat& -->
<h3 class="fn"><a name="setFormat"></a><span class="type">void</span> QSyntaxHighlighter::<span class="name">setFormat</span> ( <span class="type">int</span> <i>start</i>, <span class="type">int</span> <i>count</i>, const <span class="type"><a href="qtextcharformat.html">QTextCharFormat</a></span> &amp; <i>format</i> )<tt> [protected]</tt></h3>
<p>This function is applied to the syntax highlighter's current text block (i.e&#x2e; the text that is passed to the <a href="qsyntaxhighlighter.html#highlightBlock">highlightBlock</a>() function).</p>
<p>The specified <i>format</i> is applied to the text from the <i>start</i> position for a length of <i>count</i> characters (if <i>count</i> is 0, nothing is done). The formatting properties set in <i>format</i> are merged at display time with the formatting information stored directly in the document, for example as previously set with <a href="qtextcursor.html">QTextCursor</a>'s functions. Note that the document itself remains unmodified by the format set through this function.</p>
<p><b>See also </b><a href="qsyntaxhighlighter.html#format">format</a>() and <a href="qsyntaxhighlighter.html#highlightBlock">highlightBlock</a>().</p>
<!-- @@@setFormat -->
<!-- $$$setFormat$$$setFormatintintconstQColor& -->
<h3 class="fn"><a name="setFormat-2"></a><span class="type">void</span> QSyntaxHighlighter::<span class="name">setFormat</span> ( <span class="type">int</span> <i>start</i>, <span class="type">int</span> <i>count</i>, const <span class="type"><a href="qcolor.html">QColor</a></span> &amp; <i>color</i> )<tt> [protected]</tt></h3>
<p>This is an overloaded function.</p>
<p>The specified <i>color</i> is applied to the current text block from the <i>start</i> position for a length of <i>count</i> characters.</p>
<p>The other attributes of the current text block, e.g&#x2e; the font and background color, are reset to default values.</p>
<p><b>See also </b><a href="qsyntaxhighlighter.html#format">format</a>() and <a href="qsyntaxhighlighter.html#highlightBlock">highlightBlock</a>().</p>
<!-- @@@setFormat -->
<!-- $$$setFormat$$$setFormatintintconstQFont& -->
<h3 class="fn"><a name="setFormat-3"></a><span class="type">void</span> QSyntaxHighlighter::<span class="name">setFormat</span> ( <span class="type">int</span> <i>start</i>, <span class="type">int</span> <i>count</i>, const <span class="type"><a href="qfont.html">QFont</a></span> &amp; <i>font</i> )<tt> [protected]</tt></h3>
<p>This is an overloaded function.</p>
<p>The specified <i>font</i> is applied to the current text block from the <i>start</i> position for a length of <i>count</i> characters.</p>
<p>The other attributes of the current text block, e.g&#x2e; the font and background color, are reset to default values.</p>
<p><b>See also </b><a href="qsyntaxhighlighter.html#format">format</a>() and <a href="qsyntaxhighlighter.html#highlightBlock">highlightBlock</a>().</p>
<!-- @@@setFormat -->
</div>
      </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>