File: richtext-syntaxhighlighter.html

package info (click to toggle)
qt4-x11 4%3A4.8.2%2Bdfsg-11
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 701,696 kB
  • sloc: cpp: 2,686,179; ansic: 375,485; python: 25,859; sh: 19,349; xml: 17,091; perl: 14,765; yacc: 5,383; asm: 5,038; makefile: 1,259; lex: 555; ruby: 526; objc: 347; cs: 112; pascal: 112; php: 54; sed: 34
file content (270 lines) | stat: -rw-r--r-- 29,796 bytes parent folder | download
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
<?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" />
<!-- syntaxhighlighter.qdoc -->
  <title>Qt 4.8: Syntax Highlighter Example</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="all-examples.html">Examples</a></li>
<li>Syntax Highlighter Example</li>
    </ul>
  </div>
</div>
<div class="content mainContent">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#highlighter-class-definition">Highlighter Class Definition</a></li>
<li class="level1"><a href="#highlighter-class-implementation">Highlighter Class Implementation</a></li>
<li class="level1"><a href="#mainwindow-class-definition">MainWindow Class Definition</a></li>
<li class="level1"><a href="#mainwindow-class-implementation">MainWindow Class Implementation</a></li>
<li class="level1"><a href="#other-code-editor-features">Other Code Editor Features</a></li>
</ul>
</div>
<h1 class="title">Syntax Highlighter Example</h1>
<span class="subtitle"></span>
<!-- $$$richtext/syntaxhighlighter-description -->
<div class="descr"> <a name="details"></a>
<p>Files:</p>
<ul>
<li><a href="richtext-syntaxhighlighter-highlighter-cpp.html">richtext/syntaxhighlighter/highlighter.cpp</a></li>
<li><a href="richtext-syntaxhighlighter-highlighter-h.html">richtext/syntaxhighlighter/highlighter.h</a></li>
<li><a href="richtext-syntaxhighlighter-mainwindow-cpp.html">richtext/syntaxhighlighter/mainwindow.cpp</a></li>
<li><a href="richtext-syntaxhighlighter-mainwindow-h.html">richtext/syntaxhighlighter/mainwindow.h</a></li>
<li><a href="richtext-syntaxhighlighter-main-cpp.html">richtext/syntaxhighlighter/main.cpp</a></li>
<li><a href="richtext-syntaxhighlighter-syntaxhighlighter-pro.html">richtext/syntaxhighlighter/syntaxhighlighter.pro</a></li>
</ul>
<p>The Syntax Highlighter example shows how to perform simple syntax highlighting by subclassing the <a href="qsyntaxhighlighter.html">QSyntaxHighlighter</a> class.<p class="centerAlign"><img src="images/syntaxhighlighter-example.png" alt="" /></p><p>The Syntax Highlighter application displays C++ files with custom syntax highlighting.</p>
<p>The example consists of two classes:</p>
<ul>
<li>The <tt>Highlighter</tt> class defines and applies the highlighting rules.</li>
<li>The <tt>MainWindow</tt> widget is the application's main window.</li>
</ul>
<p>We will first review the <tt>Highlighter</tt> class to see how you can customize the <a href="qsyntaxhighlighter.html">QSyntaxHighlighter</a> class to fit your preferences, then we will take a look at the relevant parts of the <tt>MainWindow</tt> class to see how you can use your custom highlighter class in an application.</p>
<a name="highlighter-class-definition"></a>
<h2>Highlighter Class Definition</h2>
<pre class="cpp"> <span class="keyword">class</span> Highlighter : <span class="keyword">public</span> <span class="type"><a href="qsyntaxhighlighter.html">QSyntaxHighlighter</a></span>
 {
     Q_OBJECT

 <span class="keyword">public</span>:
     Highlighter(<span class="type"><a href="qtextdocument.html">QTextDocument</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);

 <span class="keyword">protected</span>:
     <span class="type">void</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="keyword">private</span>:
     <span class="keyword">struct</span> HighlightingRule
     {
         <span class="type"><a href="qregexp.html">QRegExp</a></span> pattern;
         <span class="type"><a href="qtextcharformat.html">QTextCharFormat</a></span> format;
     };
     <span class="type"><a href="qvector.html">QVector</a></span><span class="operator">&lt;</span>HighlightingRule<span class="operator">&gt;</span> highlightingRules;

     <span class="type"><a href="qregexp.html">QRegExp</a></span> commentStartExpression;
     <span class="type"><a href="qregexp.html">QRegExp</a></span> commentEndExpression;

     <span class="type"><a href="qtextcharformat.html">QTextCharFormat</a></span> keywordFormat;
     <span class="type"><a href="qtextcharformat.html">QTextCharFormat</a></span> classFormat;
     <span class="type"><a href="qtextcharformat.html">QTextCharFormat</a></span> singleLineCommentFormat;
     <span class="type"><a href="qtextcharformat.html">QTextCharFormat</a></span> multiLineCommentFormat;
     <span class="type"><a href="qtextcharformat.html">QTextCharFormat</a></span> quotationFormat;
     <span class="type"><a href="qtextcharformat.html">QTextCharFormat</a></span> functionFormat;
 };</pre>
<p>To provide your own syntax highlighting, you must subclass <a href="qsyntaxhighlighter.html">QSyntaxHighlighter</a>, reimplement the <a href="qsyntaxhighlighter.html#highlightBlock">highlightBlock()</a> function, and define your own highlighting rules.</p>
<p>We have chosen to store our highlighting rules using a private struct: A rule consists of a <a href="qregexp.html">QRegExp</a> pattern and a <a href="qtextcharformat.html">QTextCharFormat</a> instance. The various rules are then stored using a <a href="qvector.html">QVector</a>.</p>
<p>The <a href="qtextcharformat.html">QTextCharFormat</a> class provides formatting information for characters in a <a href="qtextdocument.html">QTextDocument</a> specifying the visual properties of the text, as well as information about its role in a hypertext document. In this example, we will only define the font weight and color using the <a href="qtextcharformat.html#setFontWeight">QTextCharFormat::setFontWeight</a>() and <a href="qtextformat.html#setForeground">QTextCharFormat::setForeground</a>() functions.</p>
<a name="highlighter-class-implementation"></a>
<h2>Highlighter Class Implementation</h2>
<p>When subclassing the <a href="qsyntaxhighlighter.html">QSyntaxHighlighter</a> class you must pass the parent parameter to the base class constructor. The parent is the text document upon which the syntax highligning will be applied. In this example, we have also chosen to define our highlighting rules in the constructor:</p>
<pre class="cpp"> Highlighter<span class="operator">::</span>Highlighter(<span class="type"><a href="qtextdocument.html">QTextDocument</a></span> <span class="operator">*</span>parent)
     : <span class="type"><a href="qsyntaxhighlighter.html">QSyntaxHighlighter</a></span>(parent)
 {
     HighlightingRule rule;

     keywordFormat<span class="operator">.</span>setForeground(<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>darkBlue);
     keywordFormat<span class="operator">.</span>setFontWeight(<span class="type"><a href="qfont.html">QFont</a></span><span class="operator">::</span>Bold);
     <span class="type"><a href="qstringlist.html">QStringList</a></span> keywordPatterns;
     keywordPatterns <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;\\bchar\\b&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;\\bclass\\b&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;\\bconst\\b&quot;</span>
                     <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;\\bdouble\\b&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;\\benum\\b&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;\\bexplicit\\b&quot;</span>
                     <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;\\bfriend\\b&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;\\binline\\b&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;\\bint\\b&quot;</span>
                     <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;\\blong\\b&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;\\bnamespace\\b&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;\\boperator\\b&quot;</span>
                     <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;\\bprivate\\b&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;\\bprotected\\b&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;\\bpublic\\b&quot;</span>
                     <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;\\bshort\\b&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;\\bsignals\\b&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;\\bsigned\\b&quot;</span>
                     <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;\\bslots\\b&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;\\bstatic\\b&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;\\bstruct\\b&quot;</span>
                     <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;\\btemplate\\b&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;\\btypedef\\b&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;\\btypename\\b&quot;</span>
                     <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;\\bunion\\b&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;\\bunsigned\\b&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;\\bvirtual\\b&quot;</span>
                     <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;\\bvoid\\b&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;\\bvolatile\\b&quot;</span>;
     foreach (<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&amp;</span>pattern<span class="operator">,</span> keywordPatterns) {
         rule<span class="operator">.</span>pattern <span class="operator">=</span> <span class="type"><a href="qregexp.html">QRegExp</a></span>(pattern);
         rule<span class="operator">.</span>format <span class="operator">=</span> keywordFormat;
         highlightingRules<span class="operator">.</span>append(rule);
     }</pre>
<p>First we define a keyword rule which recognizes the most common C++ keywords. We give the <tt>keywordFormat</tt> a bold, dark blue font. For each keyword, we assign the keyword and the specified format to a HighlightingRule object and append the object to our list of rules.</p>
<pre class="cpp">     classFormat<span class="operator">.</span>setFontWeight(<span class="type"><a href="qfont.html">QFont</a></span><span class="operator">::</span>Bold);
     classFormat<span class="operator">.</span>setForeground(<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>darkMagenta);
     rule<span class="operator">.</span>pattern <span class="operator">=</span> <span class="type"><a href="qregexp.html">QRegExp</a></span>(<span class="string">&quot;\\bQ[A-Za-z]+\\b&quot;</span>);
     rule<span class="operator">.</span>format <span class="operator">=</span> classFormat;
     highlightingRules<span class="operator">.</span>append(rule);

     quotationFormat<span class="operator">.</span>setForeground(<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>darkGreen);
     rule<span class="operator">.</span>pattern <span class="operator">=</span> <span class="type"><a href="qregexp.html">QRegExp</a></span>(<span class="string">&quot;\&quot;.*\&quot;&quot;</span>);
     rule<span class="operator">.</span>format <span class="operator">=</span> quotationFormat;
     highlightingRules<span class="operator">.</span>append(rule);

     functionFormat<span class="operator">.</span>setFontItalic(<span class="keyword">true</span>);
     functionFormat<span class="operator">.</span>setForeground(<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>blue);
     rule<span class="operator">.</span>pattern <span class="operator">=</span> <span class="type"><a href="qregexp.html">QRegExp</a></span>(<span class="string">&quot;\\b[A-Za-z0-9_]+(?=\\()&quot;</span>);
     rule<span class="operator">.</span>format <span class="operator">=</span> functionFormat;
     highlightingRules<span class="operator">.</span>append(rule);</pre>
<p>Then we create a format that we will apply to Qt class names. The class names will be rendered with a dark magenta color and a bold style. We specify a string pattern that is actually a regular expression capturing all Qt class names. Then we assign the regular expression and the specified format to a HighlightingRule object and append the object to our list of rules.</p>
<p>We also define highlighting rules for quotations and functions using the same approach: The patterns have the form of regular expressions and are stored in HighlightingRule objects with the associated format.</p>
<pre class="cpp">     singleLineCommentFormat<span class="operator">.</span>setForeground(<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>red);
     rule<span class="operator">.</span>pattern <span class="operator">=</span> <span class="type"><a href="qregexp.html">QRegExp</a></span>(<span class="string">&quot;//[^\n]*&quot;</span>);
     rule<span class="operator">.</span>format <span class="operator">=</span> singleLineCommentFormat;
     highlightingRules<span class="operator">.</span>append(rule);

     multiLineCommentFormat<span class="operator">.</span>setForeground(<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>red);

     commentStartExpression <span class="operator">=</span> <span class="type"><a href="qregexp.html">QRegExp</a></span>(<span class="string">&quot;/\\*&quot;</span>);
     commentEndExpression <span class="operator">=</span> <span class="type"><a href="qregexp.html">QRegExp</a></span>(<span class="string">&quot;\\*/&quot;</span>);
 }</pre>
<p>The C++ language has two variations of comments: The single line comment (<tt>//</tt>) and the multiline comment (<tt>/*..&#x2e;</tt><tt>*/</tt>). The single line comment can easily be defined through a highlighting rule similar to the previous ones. But the multiline comment needs special care due to the design of the <a href="qsyntaxhighlighter.html">QSyntaxHighlighter</a> class.</p>
<p>After a <a href="qsyntaxhighlighter.html">QSyntaxHighlighter</a> object is created, its <a href="qsyntaxhighlighter.html#highlightBlock">highlightBlock()</a> function will be called automatically whenever it is necessary by the rich text engine, highlighting the given text block. The problem appears when a comment spans several text blocks. We will take a closer look at how this problem can be solved when reviewing the implementation of the <tt>Highlighter::highlightBlock()</tt> function. At this point we only specify the multiline comment's color.</p>
<pre class="cpp"> <span class="type">void</span> Highlighter<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)
 {
     foreach (<span class="keyword">const</span> HighlightingRule <span class="operator">&amp;</span>rule<span class="operator">,</span> highlightingRules) {
         <span class="type"><a href="qregexp.html">QRegExp</a></span> expression(rule<span class="operator">.</span>pattern);
         <span class="type">int</span> index <span class="operator">=</span> expression<span class="operator">.</span>indexIn(text);
         <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> rule<span class="operator">.</span>format);
             index <span class="operator">=</span> expression<span class="operator">.</span>indexIn(text<span class="operator">,</span> index <span class="operator">+</span> length);
         }
     }</pre>
<p>The highlightBlock() function is called automatically whenever it is necessary by the rich text engine, i.e&#x2e; when there are text blocks that have changed.</p>
<p>First we apply the syntax highlighting rules that we stored in the <tt>highlightingRules</tt> vector. For each rule (i.e&#x2e; for each HighlightingRule object) we search for the pattern in the given textblock using the <a href="qstring.html#indexOf">QString::indexOf</a>() function. When the first occurrence of the pattern is found, we use the <a href="qregexp.html#matchedLength">QRegExp::matchedLength</a>() function to determine the string that will be formatted. <a href="qregexp.html#matchedLength">QRegExp::matchedLength</a>() returns the length of the last matched string, or -1 if there was no match.</p>
<p>To perform the actual formatting the <a href="qsyntaxhighlighter.html">QSyntaxHighlighter</a> class provides the <a href="qsyntaxhighlighter.html#setFormat">setFormat()</a> function. This function operates on the text block that is passed as argument to the <tt>highlightBlock()</tt> function. The specified format is applied to the text from the given start position for the given length. The formatting properties set in the given format are merged at display time with the formatting information stored directly in the document. Note that the document itself remains unmodified by the format set through this function.</p>
<p>This process is repeated until the last occurrence of the pattern in the current text block is found.</p>
<pre class="cpp">     setCurrentBlockState(<span class="number">0</span>);</pre>
<p>To deal with constructs that can span several text blocks (like the C++ multiline comment), it is necessary to know the end state of the previous text block (e.g&#x2e; &quot;in comment&quot;). Inside your <tt>highlightBlock()</tt> implementation you can query the end state of the previous text block using the <a href="qsyntaxhighlighter.html#previousBlockState">QSyntaxHighlighter::previousBlockState</a>() function. After parsing the block you can save the last state using <a href="qsyntaxhighlighter.html#setCurrentBlockState">QSyntaxHighlighter::setCurrentBlockState</a>().</p>
<p>The <a href="qsyntaxhighlighter.html#previousBlockState">previousBlockState()</a> function 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 again or until the corresponding paragraph of text is deleted.</p>
<p>In this example we have chosen to use 0 to represent the &quot;not in comment&quot; state, and 1 for the &quot;in comment&quot; state. When the stored syntax highlighting rules are applied we initialize the current block state to 0.</p>
<pre class="cpp">     <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> commentStartExpression<span class="operator">.</span>indexIn(text);</pre>
<p>If the previous block state was &quot;in comment&quot; (<tt>previousBlockState() == 1</tt>), we start the search for an end expression at the beginning of the text block. If the previousBlockState() returns 0, we start the search at the location of the first occurrence of a start expression.</p>
<pre class="cpp">     <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> commentEndExpression<span class="operator">.</span>indexIn(text<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> commentEndExpression<span class="operator">.</span>matchedLength();
         }
         setFormat(startIndex<span class="operator">,</span> commentLength<span class="operator">,</span> multiLineCommentFormat);
         startIndex <span class="operator">=</span> commentStartExpression<span class="operator">.</span>indexIn(text<span class="operator">,</span> startIndex <span class="operator">+</span> commentLength);
     }
 }</pre>
<p>When an end expression is found, we calculate the length of the comment and apply the multiline comment format. Then we search for the next occurrence of the start expression and repeat the process. If no end expression can be found in the current text block we set the current block state to 1, i.e&#x2e; &quot;in comment&quot;.</p>
<p>This completes the <tt>Highlighter</tt> class implementation; it is now ready for use.</p>
<a name="mainwindow-class-definition"></a>
<h2>MainWindow Class Definition</h2>
<p>Using a <a href="qsyntaxhighlighter.html">QSyntaxHighlighter</a> subclass is simple; just provide your application with an instance of the class and pass it the document upon which you want the highlighting to be applied.</p>
<pre class="cpp"> <span class="keyword">class</span> MainWindow : <span class="keyword">public</span> <span class="type"><a href="qmainwindow.html">QMainWindow</a></span>
 {
     Q_OBJECT

 <span class="keyword">public</span>:
     MainWindow(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);

 <span class="keyword">public</span> <span class="keyword">slots</span>:
     <span class="type">void</span> about();
     <span class="type">void</span> newFile();
     <span class="type">void</span> openFile(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&amp;</span>path <span class="operator">=</span> <span class="type"><a href="qstring.html">QString</a></span>());

 <span class="keyword">private</span>:
     <span class="type">void</span> setupEditor();
     <span class="type">void</span> setupFileMenu();
     <span class="type">void</span> setupHelpMenu();

     <span class="type"><a href="qtextedit.html">QTextEdit</a></span> <span class="operator">*</span>editor;
     Highlighter <span class="operator">*</span>highlighter;
 };</pre>
<p>In this example we declare a pointer to a <tt>Highlighter</tt> instance which we later will initialize in the private <tt>setupEditor()</tt> function.</p>
<a name="mainwindow-class-implementation"></a>
<h2>MainWindow Class Implementation</h2>
<p>The constructor of the main window is straight forward. We first set up the menus, then we initialize the editor and make it the central widget of the application. Finally we set the main window's title.</p>
<pre class="cpp"> MainWindow<span class="operator">::</span>MainWindow(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent)
     : <span class="type"><a href="qmainwindow.html">QMainWindow</a></span>(parent)
 {
     setupFileMenu();
     setupHelpMenu();
     setupEditor();

     setCentralWidget(editor);
     setWindowTitle(tr(<span class="string">&quot;Syntax Highlighter&quot;</span>));
 }</pre>
<p>We initialize and install the <tt>Highlighter</tt> object in the private setupEditor() convenience function:</p>
<pre class="cpp"> <span class="type">void</span> MainWindow<span class="operator">::</span>setupEditor()
 {
     <span class="type"><a href="qfont.html">QFont</a></span> font;
     font<span class="operator">.</span>setFamily(<span class="string">&quot;Courier&quot;</span>);
     font<span class="operator">.</span>setFixedPitch(<span class="keyword">true</span>);
     font<span class="operator">.</span>setPointSize(<span class="number">10</span>);

     editor <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qtextedit.html">QTextEdit</a></span>;
     editor<span class="operator">-</span><span class="operator">&gt;</span>setFont(font);

     highlighter <span class="operator">=</span> <span class="keyword">new</span> Highlighter(editor<span class="operator">-</span><span class="operator">&gt;</span>document());

     <span class="type"><a href="qfile.html">QFile</a></span> file(<span class="string">&quot;mainwindow.h&quot;</span>);
     <span class="keyword">if</span> (file<span class="operator">.</span>open(<span class="type"><a href="qfile.html">QFile</a></span><span class="operator">::</span>ReadOnly <span class="operator">|</span> <span class="type"><a href="qfile.html">QFile</a></span><span class="operator">::</span>Text))
         editor<span class="operator">-</span><span class="operator">&gt;</span>setPlainText(file<span class="operator">.</span>readAll());
 }</pre>
<p>First we create the font we want to use in the editor, then we create the editor itself which is an instance of the <a href="qtextedit.html">QTextEdit</a> class. Before we initialize the editor with the <tt>MainWindow</tt> class definition file, we create a <tt>Highlighter</tt> instance passing the editor's document as argument. This is the document that the highlighting will be applied to. Then we are done.</p>
<p>A <a href="qsyntaxhighlighter.html">QSyntaxHighlighter</a> object can only be installed on one document at the time, but you can easily reinstall the highlighter on another document using the <a href="qsyntaxhighlighter.html#setDocument">QSyntaxHighlighter::setDocument</a>() function. The <a href="qsyntaxhighlighter.html">QSyntaxHighlighter</a> class also provides the <a href="qsyntaxhighlighter.html#document">document()</a> function which returns the currently set document.</p>
<a name="other-code-editor-features"></a>
<h2>Other Code Editor Features</h2>
<p>It is possible to implement parenthesis matching with <a href="qsyntaxhighlighter.html">QSyntaxHighlighter</a>. The &quot;Matching Parentheses with <a href="qsyntaxhighlighter.html">QSyntaxHighlighter</a>&quot; article in Qt Quarterly 31 (<a href="http://doc.qt.nokia.com/qq/">http://doc.qt.nokia.com/qq/</a>) implements this. We also have the <a href="widgets-codeeditor.html">Code Editor Example</a>, which shows how to implement line numbers and how to highlight the current line.</p>
</div>
<!-- @@@richtext/syntaxhighlighter -->
  <div class="ft">
    <span></span>
  </div>
</div> 
<div class="footer">
    <p>
      <acronym title="Copyright">&copy;</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>