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.
Using QPlainTextEdit as a Display Widget Text can be inserted using the QTextCursor class or using the convenience functions insertPlainText(), appendPlainText() or paste().
QTextEdit does not have any text() method, if you want to get the text you must use toPlainText() , if you want to clean the text it is better to use clear() since it makes it more readable.
If you want to provide your users with an editable rich text editor, use QTextEdit. If you want a text browser without hypertext navigation use QTextEdit, and use QTextEdit::setReadOnly() to disable editing. If you just need to display a small piece of rich text use QLabel.
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 behindQTextEdit
andQTextDocument
. Its performance benefits overQTextEdit
stem mostly from using a different and simplified text layout calledQPlainTextDocumentLayout
on the text document (seeQTextDocument::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 (seesetMaximumBlockCount()
).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With