QTextEdit vs QPlainTextEdit

Markus Meskanen picture Markus Meskanen · Jul 4, 2013 · Viewed 34.6k times · Source

What's the difference between QTextEdit and QPlainTextEdit, why use one over the other?

I'm coding a text editor as an exercice to learn Qt5, and now I'm wondering whether to use QTextEdit or QPlainTextEdit. So far I've only found out that you can display images in QTextEdit, but other than that they look somewhat identical to me. My text editor should support some basic syntax highlighting (probably using textChanged() signal), but that's pretty much as far as the requirements go.

Google searches for "QTextEdit vs QPlainTextEdit" and "QTextEdit compared to QPlainTextEdit" didn't give me any decent results that would compare the two classes.

Answer

Bakuriu picture Bakuriu · Jul 4, 2013

From Qt's documentation:

QPlainTextEdit is an advanced viewer/editor supporting plain text. It is optimized to handle large documents and to respond quickly to user input.

QPlainText uses very much the same technology and concepts as QTextEdit, but is optimized for plain text handling.

QPlainTextEdit works on paragraphs and characters. A paragraph is a formatted string which is word-wrapped to fit into the width of the widget. By default when reading plain text, one newline signifies a paragraph. A document consists of zero or more paragraphs. Paragraphs are separated by hard line breaks. Each character within a paragraph has its own attributes, for example, font and color.

And later on:

Differences to QTextEdit

QPlainTextEdit is a thin class, implemented by using most of the technology that is behind QTextEdit and QTextDocument. Its performance benefits over QTextEdit stem mostly from using a different and simplified text layout called QPlainTextDocumentLayout on the text document (see QTextDocument::setDocumentLayout()). The plain text document layout does not support tables nor embedded frames, and replaces a pixel-exact height calculation with a line-by-line respectively paragraph-by-paragraph scrolling approach. This makes it possible to handle significantly larger documents, and still resize the editor with line wrap enabled in real time. It also makes for a fast log viewer (see setMaximumBlockCount()).

So the difference is that QPlainTextEdit is optimized for handling plain text, and can be used even with very large plain text files. Also the way text is formatted is simpler.

If you plan to support only plain texts, then QPlainTextEdit is the right choice.