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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:12:41  来源:igfitidea点击:

QTextEdit vs QPlainTextEdit

c++qtqt5qtexteditqplaintextedit

提问by Markus Meskanen

What's the difference between QTextEditand 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 QTextEditor 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 的练习,现在我想知道是否使用QTextEditQPlainTextEdit. 到目前为止,我只发现您可以在 中显示图像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 的文档

QPlainTextEditis 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.

QPlainTextEditworks 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

QPlainTextEditis a thin class, implemented by using most of the technology that is behind QTextEditand QTextDocument. Its performance benefits over QTextEditstem mostly from using a different and simplified text layout called QPlainTextDocumentLayouton 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()).

与 QTextEdit 的区别

QPlainTextEdit是一个瘦类,通过使用QTextEdit和背后的大部分技术实现QTextDocument。它的性能优势QTextEdit主要源于使用QPlainTextDocumentLayout对文本文档调用的不同且简化的文本布局(请参阅 参考资料QTextDocument::setDocumentLayout())。纯文本文档布局不支持表格或嵌入框架,并用逐行逐段滚动方法替换像素精确高度计算。这使得处理明显更大的文档成为可能,并且仍然可以实时调整编辑器的大小并启用换行。它还可以实现快速的日志查看器(请参阅 参考资料setMaximumBlockCount())。

So the difference is that QPlainTextEditis 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 QPlainTextEditis the right choice.

如果您打算只支持纯文本,那么QPlainTextEdit是正确的选择。