1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
<?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/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
<div class="content">
<a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
</div>
<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>
<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 <QSyntaxHighlighter></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 & <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 & <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 & <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 & <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 & <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">></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. 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">&</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">"\\bMy[A-Za-z]+\\b"</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">></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>*...*</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. "in comment").</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 "in comment":</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">"/\\*"</span>);
<span class="type"><a href="qregexp.html">QRegExp</a></span> endExpression(<span class="string">"\\*/"</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">></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> & <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. 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">&</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">"\\bMy[A-Za-z]+\\b"</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">></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>*...*</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. "in comment").</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 "in comment". 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> & <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. 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"><</span>ParenthesisInfo<span class="operator">></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> & <i>format</i> )<tt> [protected]</tt></h3>
<p>This function is applied to the syntax highlighter's current text block (i.e. 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> & <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. 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> & <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. 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 class="ft">
<span></span>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2012 Nokia Corporation 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>
Nokia, Qt and their respective logos are trademarks of Nokia Corporation
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>
</body>
</html>
|