title: Custom text (code) areas in QTextEdit
body: <p>I am interested in creating a text object type (inheriting <a href="http://doc.qt.io/qt-5/qtextobjectinterface.html" rel="nofollow">QTextObjectInterface</a>) that behaves like a code area:</p>
  
  <ul>
  <li>distinctive background</li>
  <li>border</li>
  <li>fixed-width font</li>
  <li>editable content</li>
  <li>the instances need to be identifiable to code, so that content inside them may be extracted (separate code from surrounding content)</li>
  <li>saving / loading (from regular html files)</li>
  <li>syntax highlighting would be a plus, but is not really required</li>
  </ul>
  
  <p>The other areas of the document would need to behave the usual way (font properties editable, colors editable, etc).</p>
  
  <hr>
  
  <p>Qt provides <a href="http://doc.qt.io/qt-5/qtsvg-richtext-textobject-example.html" rel="nofollow">an example</a> for implementing custom text objects with <a href="http://doc.qt.io/qt-5/qtextedit.html" rel="nofollow">QTextEdit</a>. This looks like the hard way, since new text object can't make use of the existing infrastructure inside QTextEdit / <a href="http://doc.qt.io/qt-5/qtextdocument.html" rel="nofollow">QTextDocument</a>. </p>
  
  <p><a href="http://doc.qt.io/qt-5/qtextobject.html" rel="nofollow">QTextObject</a> is </p>
  
  <blockquote>
    <p>a base class for different kinds of objects that can group parts of a <a href="http://doc.qt.io/qt-5/qtextdocument.html" rel="nofollow">QTextDocument</a> together</p>
  </blockquote>
  
  <p>so inheriting it may be a choice, but neither its source files in Qt SDK package nor Google searches revealed helpful information.</p>
  
  <p><a href="http://doc.qt.io/qt-5/qtextframe.html" rel="nofollow">QTextFrame</a> inherits <a href="http://doc.qt.io/qt-5/qtextobject.html" rel="nofollow">QTextObject</a> so, again, it may be a feasible base class if some hints about this path are to be found.</p>
  
  <hr>
  
  <p>In an simple HTML file all this (except syntax highlighting) would be easy. <a href="http://doc.qt.io/qt-5/qtextedit.html" rel="nofollow">QTextEdit</a> takes html as input and is able to export html, but the structure is lost in the process. </p>
  
  <pre><code>&lt;code class="code-sample"&gt;
    int i = 0;
  &lt;/code&gt;
  </code></pre>
  
  <p><a href="http://doc.qt.io/qt-5/qwebview.html" rel="nofollow">QWebView</a> is read-only, by the way. It advertises that:</p>
  
  <blockquote>
    <p>Parts of HTML documents can be editable for example through the contenteditable attribute on HTML elements.</p>
  </blockquote>
  
  <hr>
  
  <p>There may be other platforms where this is readily available, but the text editor needs to be used inside Qt Creator as a plug-in, so using Qt framework makes sense. </p>
  
  <p>Bottom line: how does one implement code areas in a <a href="http://doc.qt.io/qt-5/qtextedit.html" rel="nofollow">QTextEdit</a> widget?</p>
  
  <hr>
  
  <p>Later edits:</p>
  
  <ul>
  <li>using Qt sdk from trunk (identifies itself as 4.8.4)</li>
  <li>Qt Creator from trunk (Qt Creator 2.6.81)</li>
  </ul>