我有一个 GUI 应用程序,其主要部分是QPlainTextEdit。它用于显示应用程序的日志,因此相关的文本会逐行无限增长。
由于应用程序打算运行很长时间,我需要限制将为此日志分配的内存。因此,我希望有一些maxNumLines
或maxNumCharacters
参数来确保在到达时将截断历史记录,即在添加新行时将删除标题行(也称为日志轮换)。
为了实现这一点,我找到了功能
// get the associated text
QString toPlainText () const
// set the associated text
void setPlainText ( const QString & text )
因此,像这样未经测试的代码可能会成功:
QString &tmp = pte.toPlainText();
while (tmp.size() > maxNumCharacters) {
// remove lines from the head of the string until the desired size is reached
// removes nothing if "\n" could not be found
tmp.remove(0, tmp.indexOf("\n")+1);
}
pte.setPlainText( tmp );
这是从 中删除第一行的方法QPlainTextEdit
吗?是否可能有其他 Qt Text GUI 元素更适合此任务(设置最大行数并在列表的开头截断),例如以某种方式显示QStringList
我可以存储行的 a (我可以轻松地erase(0)
)?
或者 QPlainTextEdit 最终是否最终为相关 QString 的大小实现了这样的上限?