sidnical
sidnical

Reputation: 459

pyqt5 textedit delete lines as they move past specified line

I'd like to setup the PYQT Qtextedit widget and use it to monitor another applications activity log(like tail -f on Linux). Long term I worry about it running for too long and using a lot of ram with the text that builds up. Is it possible to set a limit so that text moving past line x gets deleted? From what I've found it seems to require custom work and I'd like to find a limiter setting if one exists.

Upvotes: 4

Views: 1376

Answers (1)

eyllanesc
eyllanesc

Reputation: 243907

QPlainTextEdit is an advanced viewer/editor supporting plain text. It is optimized to handle large documents and to respond quickly to user input.

To limit the number of visible lines you must use setMaximumBlockCount, in the following example I show the use:

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

counter = 0

def addText():
    global counter
    w.appendHtml("<font size=\"3\" color=\"red\">{}</font>".format(counter))
    counter += 1

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = QPlainTextEdit()

    timer = QTimer()
    timer.timeout.connect(addText)
    timer.start(1000)
    w.setMaximumBlockCount(4)
    w.show()
    sys.exit(app.exec_())

If you want to use fonts you can do it easily using HTML.

Upvotes: 5

Related Questions