Sophie

Sophie

distrib > Mageia > 6 > armv5tl > media > core-updates > by-pkgid > 768f7d9f703884aa2562bf0a651086df > files > 4241

qtbase5-doc-5.9.4-1.1.mga6.noarch.rpm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- syntaxhighlighter.qdoc -->
  <title>Syntax Highlighter Example | Qt Widgets 5.9</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td >Qt 5.9</td><td ><a href="qtwidgets-index.html">Qt Widgets</a></td><td ><a href="examples-richtext.html">Rich Text Examples</a></td><td >Syntax Highlighter Example</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.9.4 Reference Documentation</td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<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>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Syntax Highlighter Example</h1>
<span class="subtitle"></span>
<!-- $$$richtext/syntaxhighlighter-description -->
<div class="descr"> <a name="details"></a>
<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 <code>Highlighter</code> class defines and applies the highlighting rules.</li>
<li>The <code>MainWindow</code> widget is the application's main window.</li>
</ul>
<p>We will first review the <code>Highlighter</code> class to see how you can customize the <a href="../qtgui/qsyntaxhighlighter.html">QSyntaxHighlighter</a> class to fit your preferences, then we will take a look at the relevant parts of the <code>MainWindow</code> class to see how you can use your custom highlighter class in an application.</p>
<a name="highlighter-class-definition"></a>
<h2 id="highlighter-class-definition">Highlighter Class Definition</h2>
<pre class="cpp">

  <span class="keyword">class</span> Highlighter : <span class="keyword">public</span> <span class="type"><a href="../qtgui/qsyntaxhighlighter.html">QSyntaxHighlighter</a></span>
  {
      Q_OBJECT

  <span class="keyword">public</span>:
      Highlighter(<span class="type"><a href="../qtgui/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="../qtcore/qstring.html">QString</a></span> <span class="operator">&amp;</span>text) override;

  <span class="keyword">private</span>:
      <span class="keyword">struct</span> HighlightingRule
      {
          <span class="type"><a href="../qtcore/qregularexpression.html">QRegularExpression</a></span> pattern;
          <span class="type"><a href="../qtgui/qtextcharformat.html">QTextCharFormat</a></span> format;
      };
      <span class="type"><a href="../qtcore/qvector.html">QVector</a></span><span class="operator">&lt;</span>HighlightingRule<span class="operator">&gt;</span> highlightingRules;

      <span class="type"><a href="../qtcore/qregularexpression.html">QRegularExpression</a></span> commentStartExpression;
      <span class="type"><a href="../qtcore/qregularexpression.html">QRegularExpression</a></span> commentEndExpression;

      <span class="type"><a href="../qtgui/qtextcharformat.html">QTextCharFormat</a></span> keywordFormat;
      <span class="type"><a href="../qtgui/qtextcharformat.html">QTextCharFormat</a></span> classFormat;
      <span class="type"><a href="../qtgui/qtextcharformat.html">QTextCharFormat</a></span> singleLineCommentFormat;
      <span class="type"><a href="../qtgui/qtextcharformat.html">QTextCharFormat</a></span> multiLineCommentFormat;
      <span class="type"><a href="../qtgui/qtextcharformat.html">QTextCharFormat</a></span> quotationFormat;
      <span class="type"><a href="../qtgui/qtextcharformat.html">QTextCharFormat</a></span> functionFormat;
  };

</pre>
<p>To provide your own syntax highlighting, you must subclass <a href="../qtgui/qsyntaxhighlighter.html">QSyntaxHighlighter</a>, reimplement the <a href="../qtgui/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="../qtcore/qregularexpression.html">QRegularExpression</a> pattern and a <a href="../qtgui/qtextcharformat.html">QTextCharFormat</a> instance. The various rules are then stored using a <a href="../qtcore/qvector.html">QVector</a>.</p>
<p>The <a href="../qtgui/qtextcharformat.html">QTextCharFormat</a> class provides formatting information for characters in a <a href="../qtgui/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="../qtgui/qtextcharformat.html#setFontWeight">QTextCharFormat::setFontWeight</a>() and <a href="../qtgui/qtextformat.html#setForeground">QTextCharFormat::setForeground</a>() functions.</p>
<a name="highlighter-class-implementation"></a>
<h2 id="highlighter-class-implementation">Highlighter Class Implementation</h2>
<p>When subclassing the <a href="../qtgui/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 highlighting 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="../qtgui/qtextdocument.html">QTextDocument</a></span> <span class="operator">*</span>parent)
      : <span class="type"><a href="../qtgui/qsyntaxhighlighter.html">QSyntaxHighlighter</a></span>(parent)
  {
      HighlightingRule rule;

      keywordFormat<span class="operator">.</span>setForeground(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>darkBlue);
      keywordFormat<span class="operator">.</span>setFontWeight(<span class="type"><a href="../qtgui/qfont.html">QFont</a></span><span class="operator">::</span>Bold);
      <span class="type"><a href="../qtcore/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> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;\\bbool\\b&quot;</span>;
      foreach (<span class="keyword">const</span> <span class="type"><a href="../qtcore/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="../qtcore/qregularexpression.html">QRegularExpression</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 <code>keywordFormat</code> 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="../qtgui/qfont.html">QFont</a></span><span class="operator">::</span>Bold);
      classFormat<span class="operator">.</span>setForeground(<span class="type"><a href="../qtcore/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="../qtcore/qregularexpression.html">QRegularExpression</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="../qtcore/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="../qtcore/qregularexpression.html">QRegularExpression</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="../qtcore/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="../qtcore/qregularexpression.html">QRegularExpression</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="../qtcore/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="../qtcore/qregularexpression.html">QRegularExpression</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="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>red);

      commentStartExpression <span class="operator">=</span> <span class="type"><a href="../qtcore/qregularexpression.html">QRegularExpression</a></span>(<span class="string">&quot;/\\*&quot;</span>);
      commentEndExpression <span class="operator">=</span> <span class="type"><a href="../qtcore/qregularexpression.html">QRegularExpression</a></span>(<span class="string">&quot;\\*/&quot;</span>);
  }

</pre>
<p>The C++ language has two variations of comments: The single line comment (<code>//</code>) and the multiline comment (<code>/*..&#x2e;*</code><code>/</code>). 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="../qtgui/qsyntaxhighlighter.html">QSyntaxHighlighter</a> class.</p>
<p>After a <a href="../qtgui/qsyntaxhighlighter.html">QSyntaxHighlighter</a> object is created, its <a href="../qtgui/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 <code>Highlighter::highlightBlock()</code> 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="../qtcore/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="../qtcore/qregularexpressionmatchiterator.html">QRegularExpressionMatchIterator</a></span> matchIterator <span class="operator">=</span> rule<span class="operator">.</span>pattern<span class="operator">.</span>globalMatch(text);
          <span class="keyword">while</span> (matchIterator<span class="operator">.</span>hasNext()) {
              <span class="type"><a href="../qtcore/qregularexpressionmatch.html">QRegularExpressionMatch</a></span> match <span class="operator">=</span> matchIterator<span class="operator">.</span>next();
              setFormat(match<span class="operator">.</span>capturedStart()<span class="operator">,</span> match<span class="operator">.</span>capturedLength()<span class="operator">,</span> rule<span class="operator">.</span>format);
          }
      }

</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 <code>highlightingRules</code> vector. For each rule (i.e&#x2e; for each HighlightingRule object) we search for the pattern in the given text block using the <a href="../qtcore/qstring.html#indexOf-1">QString::indexOf</a>() function. When the first occurrence of the pattern is found, we use the <a href="../qtcore/qregularexpressionmatch.html#capturedLength-1">QRegularExpressionMatch::capturedLength</a>() function to determine the string that will be formatted. <a href="../qtcore/qregularexpressionmatch.html#capturedLength-1">QRegularExpressionMatch::capturedLength</a>() returns the length of the last matched string, or 0 if there was no match.</p>
<p>To perform the actual formatting the <a href="../qtgui/qsyntaxhighlighter.html">QSyntaxHighlighter</a> class provides the <a href="../qtgui/qsyntaxhighlighter.html#setFormat-1">setFormat()</a> function. This function operates on the text block that is passed as argument to the <code>highlightBlock()</code> 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 <code>highlightBlock()</code> implementation you can query the end state of the previous text block using the <a href="../qtgui/qsyntaxhighlighter.html#previousBlockState">QSyntaxHighlighter::previousBlockState</a>() function. After parsing the block you can save the last state using <a href="../qtgui/qsyntaxhighlighter.html#setCurrentBlockState">QSyntaxHighlighter::setCurrentBlockState</a>().</p>
<p>The <a href="../qtgui/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="../qtgui/qsyntaxhighlighter.html#setCurrentBlockState">setCurrentBlockState()</a> function. Once the state is set, the <a href="../qtgui/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> text<span class="operator">.</span>indexOf(commentStartExpression);

</pre>
<p>If the previous block state was &quot;in comment&quot; (<code>previousBlockState() == 1</code>), 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"><a href="../qtcore/qregularexpressionmatch.html">QRegularExpressionMatch</a></span> match <span class="operator">=</span> commentEndExpression<span class="operator">.</span>match(text<span class="operator">,</span> startIndex);
          <span class="type">int</span> endIndex <span class="operator">=</span> match<span class="operator">.</span>capturedStart();
          <span class="type">int</span> commentLength <span class="operator">=</span> <span class="number">0</span>;
          <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> match<span class="operator">.</span>capturedLength();
          }
          setFormat(startIndex<span class="operator">,</span> commentLength<span class="operator">,</span> multiLineCommentFormat);
          startIndex <span class="operator">=</span> text<span class="operator">.</span>indexOf(commentStartExpression<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 <code>Highlighter</code> class implementation; it is now ready for use.</p>
<a name="mainwindow-class-definition"></a>
<h2 id="mainwindow-class-definition">MainWindow Class Definition</h2>
<p>Using a <a href="../qtgui/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="../qtcore/qstring.html">QString</a></span> <span class="operator">&amp;</span>path <span class="operator">=</span> <span class="type"><a href="../qtcore/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 <code>Highlighter</code> instance which we later will initialize in the private <code>setupEditor()</code> function.</p>
<a name="mainwindow-class-implementation"></a>
<h2 id="mainwindow-class-implementation">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 <code>Highlighter</code> 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="../qtgui/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="../qtcore/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="../qtcore/qfile.html">QFile</a></span><span class="operator">::</span>ReadOnly <span class="operator">|</span> <span class="type"><a href="../qtcore/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 <code>MainWindow</code> class definition file, we create a <code>Highlighter</code> 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="../qtgui/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="../qtgui/qsyntaxhighlighter.html#setDocument">QSyntaxHighlighter::setDocument</a>() function. The <a href="../qtgui/qsyntaxhighlighter.html">QSyntaxHighlighter</a> class also provides the <a href="../qtgui/qsyntaxhighlighter.html#document">document()</a> function which returns the currently set document.</p>
<a name="other-code-editor-features"></a>
<h2 id="other-code-editor-features">Other Code Editor Features</h2>
<p>It is possible to implement parenthesis matching with <a href="../qtgui/qsyntaxhighlighter.html">QSyntaxHighlighter</a>. The &quot;Matching Parentheses with <a href="../qtgui/qsyntaxhighlighter.html">QSyntaxHighlighter</a>&quot; article in Qt Quarterly 31 (<a href="http://doc.qt.io/archives/qq/">http://doc.qt.io/archives/qq/</a>) implements this. We also have the <a href="qtwidgets-widgets-codeeditor-example.html">Code Editor Example</a>, which shows how to implement line numbers and how to highlight the current line.</p>
<p>Files:</p>
<ul>
<li><a href="qtwidgets-richtext-syntaxhighlighter-highlighter-cpp.html">richtext/syntaxhighlighter/highlighter.cpp</a></li>
<li><a href="qtwidgets-richtext-syntaxhighlighter-highlighter-h.html">richtext/syntaxhighlighter/highlighter.h</a></li>
<li><a href="qtwidgets-richtext-syntaxhighlighter-mainwindow-cpp.html">richtext/syntaxhighlighter/mainwindow.cpp</a></li>
<li><a href="qtwidgets-richtext-syntaxhighlighter-mainwindow-h.html">richtext/syntaxhighlighter/mainwindow.h</a></li>
<li><a href="qtwidgets-richtext-syntaxhighlighter-main-cpp.html">richtext/syntaxhighlighter/main.cpp</a></li>
<li><a href="qtwidgets-richtext-syntaxhighlighter-syntaxhighlighter-pro.html">richtext/syntaxhighlighter/syntaxhighlighter.pro</a></li>
</ul>
</div>
<!-- @@@richtext/syntaxhighlighter -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2017 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br>    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.<br>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>