c++ - QTextEdit 与 QPlainTextEdit

标签 c++ qt qt5 qtextedit qplaintextedit

QTextEditQPlainTextEdit 有什么区别,为什么要用一个?

我正在编写一个文本编辑器作为学习 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 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()).

所以区别在于 QPlainTextEdit 为处理纯文本进行了优化,甚至可以用于非常大的纯文本文件。文本的格式化方式也更简单。

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

关于c++ - QTextEdit 与 QPlainTextEdit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17466046/

相关文章:

python - PyQt5:Gtk-CRITICAL **:IA__gtk_widget_style_get:断言 'GTK_IS_WIDGET (widget)' 失败

c++ - 与应用程序的链接错误

javascript - openlayers 功能消失图层样式

ubuntu - 从 Ubuntu 存储库安装 Qt5 webenginewidgets 模块

qt - Qt3D 是 Qt5 的一部分吗?

c++ - 如何将 'const boost::filesystem2::path' 变成 'const char *' ?

c++ - 扫描进程并在找到特定进程时执行某些操作

c++ - Recvfrom() 挂起——当服务器关闭时如何处理这个问题

python - PyQT:PushButton 在禁用时接收命令