QTextEdit
和 QPlainTextEdit
有什么区别,为什么要用一个?
我正在编写一个文本编辑器作为学习 Qt5 的练习,现在我想知道是使用 QTextEdit
还是 QPlainTextEdit
。
到目前为止,我只发现您可以在 QTextEdit
中显示图像,但除此之外它们看起来与我有些相同。
我的文本编辑器应该支持一些基本的语法高亮显示(可能使用 textChanged()
信号),但这几乎满足要求。
Google 搜索 “QTextEdit 与 QPlainTextEdit” 和 “QTextEdit 与 QPlainTextEdit 相比” 没有给我任何可以比较这两个类的体面结果。
最佳答案
来自 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.
后来:
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()
).
所以区别在于 QPlainTextEdit
为处理纯文本进行了优化,甚至可以用于非常大的纯文本文件。文本的格式化方式也更简单。
如果您打算只支持纯文本,那么 QPlainTextEdit
是正确的选择。
关于c++ - QTextEdit 与 QPlainTextEdit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17466046/