Important: Please read the Qt Code of Conduct -

Custom text (code) areas in QTextEdit

  • I am interested in creating a text object type (inheriting QTextObjectInterface) that behaves like a code area:

    • distinctive background
    • border
    • fixed-width font
    • editable content
    • the instances need to be identifiable to code, so that content inside them may be extracted (separate code from surrounding content)
    • saving / loading (from regular html files)
    • syntax highlighting would be a plus, but is not really required

    The other areas of the document would need to behave the usual way (font properties editable, colors editable, etc).

    Qt provides an example for implementing custom text objects with QTextEdit. This looks like the hard way, since new text object can't make use of the existing infrastructure inside QTextEdit / QTextDocument.

    QTextObject is

    bq. a base class for different kinds of objects that can group parts of a QTextDocument together

    so inheriting it may be a choice, but neither its source files in Qt SDK package nor Google searches revealed helpful information.

    QTextFrame inherits QTextObject so, again, it may be a feasible base class if some hints about this path are to be found.

    In an simple HTML file all this (except syntax highlighting) would be easy. QTextEdit takes html as input and is able to export html, but the structure is lost in the process.

    @<code class="code-sample">
    int i = 0;

    QWebView is read-only, by the way. It advertises that:

    bq. Parts of HTML documents can be editable for example through the contenteditable attribute on HTML elements.

    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.

    Bottom line: how does one implement code areas in a QTextEdit widget?

    using Qt sdk from trunk (identifies itself as 4.8.4)
    Qt Creator from trunk (Qt Creator 2.6.81)

    Note: This is a duplicate of "a currently unanswered question": posted on StackOverflow. I post it here, too, in the hope that it will have better luck.

    Thank you!

Log in to reply