C++ QTextEdit 与 QPlainTextEdit
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17466046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
QTextEdit vs QPlainTextEdit
提问by Markus Meskanen
What's the difference between QTextEdit
and QPlainTextEdit
, why use one over the other?
QTextEdit
和之间有什么区别QPlainTextEdit
,为什么要使用一个?
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.
我正在编写一个文本编辑器作为学习 Qt5 的练习,现在我想知道是否使用QTextEdit
或QPlainTextEdit
. 到目前为止,我只发现您可以在 中显示图像QTextEdit
,但除此之外,它们对我来说看起来有些相同。我的文本编辑器应该支持一些基本的语法突出显示(可能使用textChanged()
信号),但这几乎就需求而言。
Google searches for "QTextEdit vs QPlainTextEdit"and "QTextEdit compared to QPlainTextEdit"didn't give me any decent results that would compare the two classes.
谷歌搜索“QTextEdit vs QPlainTextEdit”和“QTextEdit与QPlainTextEdit相比”并没有给我任何可以比较这两个类的不错的结果。
采纳答案by Bakuriu
From Qt's documentation:
从Qt 的文档:
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.
QPlainTextEdit
是支持纯文本的高级查看器/编辑器。它经过优化,可处理大型文档并快速响应用户输入。QPlainText 使用与 非常相同的技术和概念
QTextEdit
,但针对纯文本处理进行了优化。
QPlainTextEdit
适用于段落和字符。一个段落是一个格式化的字符串,它被自动换行以适应小部件的宽度。默认情况下,阅读纯文本时,换行符表示一个段落。一个文档由零个或多个段落组成。段落由硬换行符分隔。段落中的每个字符都有自己的属性,例如字体和颜色。
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()
).
与 QTextEdit 的区别
QPlainTextEdit
是一个瘦类,通过使用QTextEdit
和背后的大部分技术实现QTextDocument
。它的性能优势QTextEdit
主要源于使用QPlainTextDocumentLayout
对文本文档调用的不同且简化的文本布局(请参阅 参考资料QTextDocument::setDocumentLayout()
)。纯文本文档布局不支持表格或嵌入框架,并用逐行逐段滚动方法替换像素精确高度计算。这使得处理明显更大的文档成为可能,并且仍然可以实时调整编辑器的大小并启用换行。它还可以实现快速的日志查看器(请参阅 参考资料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.
所以区别在于它QPlainTextEdit
针对处理纯文本进行了优化,甚至可以用于非常大的纯文本文件。文本格式化的方式也更简单。
If you plan to support only plain texts, then QPlainTextEdit
is the right choice.
如果您打算只支持纯文本,那么QPlainTextEdit
是正确的选择。