QPlainTextEdit truncates linquise history

I have a GUI application, the main part of which is QPlainTextEdit . It is used to display the application log, and as such, the associated text grows line by line to infinity.

Since the application is designed to work for a very long time, I need to limit the memory that will be allocated for this log. Therefore, I want to have some parameter maxNumLines or maxNumCharacters , which ensures that the history will be truncated upon reaching, i.e. Headers will be deleted as new lines are added (rotation of the aka journal).

For this, I found functions

 // get the associated text QString toPlainText () const // set the associated text void setPlainText ( const QString & text ) 

Therefore, something like this untested code is likely to do the trick:

 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 ); 

Is it possible to remove the first row from QPlainTextEdit ? Perhaps there are other Qt text GUI elements that are better suited for this task (set the maximum number of lines and truncate at the top of the list), for example. somehow display a QStringList in which I could store the strings (st I could easily erase(0) )?

Or does QPlainTextEdit eventually implement such an upper bound for the size of the associated QString?

+7
source share
2 answers

Obviously, the maximumBlockCount property is exactly what I need:

If you want to limit the total number of paragraphs in QPlainTextEdit , as it is, for example, useful in the log viewer, you can use the maximumBlockCount property. The combination of setMaximumBlockCount() and appendPlainText() turns QPlainTextEdit into an efficient view of the log text.

For reference:

+10
source

I had exactly the same problem months ago and ended up using QListView . Although the use of the model / view / delegate architecture is a bit more complex, in the long run it improves significantly. For example, when the basic architecture is installed, adding a filter that displays only error records or warnings becomes trivial or creates a delegate, so the background of the error records is colored red is also simple.

+8
source

All Articles