Important: Please read the Qt Code of Conduct -

Managing memory in a QPlainTextEdit widget

  • I have a QPlainTextEdit widget which I use as a log window for a manufacturing app. The number of events logged to this window are large. I would like to limit the window to, say, the last 10,000 lines. First, how can I track the memory usage of a widget? Second, is there a way to clip or limit the number of lines of data in a widget like this. Thanks!


  • Moderators

    Memory usage in bytes should be around twice the number of characters in the edit since QString is using UTF16 to represent unicode values.

    Of course you can remove lines you do not care about anymore: Just delete anything but the last 10k of lines whenever you add a new line.

  • In addition to Tobias answer I can say that it will be better to create a subclass of QPlainTextEdit which will do this truncation internally. Just to not copy/paste this operation around a project.

  • Moderators

    Denis: I hope nobody would get the idea to repeat the truncating code all over the place!

  • Maybe @QPlainTextEdit::setMaximumBlockCount()@ fit your needs. It limits the number of paragraphs for you, by removing the oldest. As documentation says, it is "useful in a log viewer".

  • Calerto,
    Thank you. That was exactly what I was looking for! :-)


Log in to reply