高级富文本处理
处理大文件
Qt 对用于文本处理的文件大小没有限制。在大多数情况下,这不会造成问题。 然而,对于特别大的文件,您可能会遇到应用程序无响应或内存不足的情况。可加载的文件大小取决于您的硬件以及 Qt 和您自己应用程序的实现。
如果您遇到此问题,建议您解决以下问题:
- 应考虑将大段落拆分为较小的段落,因为 Qt XML 对小段落的处理效果更好。您还可以在固定间隔处插入换行符,这样在QTextEdit 中显示时,效果与一个大段落并无二致。
- 您可以通过maximumBlockCount() 减少QTextDocument 中的块数量。对于QTextEdit 而言,文档的大小仅取决于块的数量。
- 向文本编辑器添加文本时,最好将其添加到编辑块中(参见下例)。这样,文本编辑器就不需要一次性构建整个文档结构。
我们从列表中举一个后一种技术的示例。假设文本编辑框处于可见状态。
textEdit.show();
textCursor.beginEditBlock();
for (int i = 0; i < 1000; ++i) {
textCursor.insertBlock();
textCursor.insertText(paragraphText.at(i));
}
textCursor.endEditBlock();© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.