高级富文本处理
处理大文件
Qt 不限制用于文本处理的文件大小。在大多数情况下,这不会造成问题。但对于特别大的文件,您可能会遇到应用程序反应迟钝或内存不足的问题。您可以加载的文件大小取决于您的硬件以及 Qt 和您自己的应用程序的实现。
如果您遇到这个问题,我们建议您解决以下问题:
- 您应该考虑将大段落分割成小段落,因为 Qt 能更好地处理小段落。您也可以按一定的间隔插入换行符,这样看起来就像在QTextEdit 中插入一个大段落一样。
- 您可以使用maximumBlockCount() 减少QTextDocument 中的块数量。就QTextEdit 而言,文档的大小取决于块的数量。
- 在文本编辑中添加文本时,在编辑块中添加文本是一种优势(见下文示例)。这样做的结果是,文本编辑不需要一次性构建整个文档结构。
我们从列表中举例说明后一种技术。我们假设文本编辑是可见的。
textEdit.show(); textCursor.beginEditBlock(); for (int i = 0; i < 1000; ++i) { textCursor.insertBlock(); textCursor.insertText(paragraphText.at(i)); } textCursor.endEditBlock();
© 2025 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.