4

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.

Tejas Thakar
  • 585
  • 5
  • 19
sidnical
  • 421
  • 1
  • 6
  • 23
  • Can you should what you've tried? – Alex Huszagh Jul 12 '17 at 04:44
  • 1
    What you want is to set a maximum number of lines in QTextEdit, and if there is a new line you must delete the first ones that were written, am I correct? – eyllanesc Jul 12 '17 at 04:58
  • @eyllanesc yes that is exactly what I'm after. – sidnical Jul 12 '17 at 05:07
  • @AlexanderHuszagh Im new to pyqt and this i my first approach to the text editor. I've been doing research before I start learning about it and I wasn't finding a way to do what I wanted so I thought i'd ask. If I should be using a different widget i'd like to find out before going through test with the qtextedit widget. I'll learn all about the text editor but for my current project I want to make sure im using the right tools. – sidnical Jul 12 '17 at 05:14
  • If you use QPlainTextEdit I can give you a simple option, would it be okay if I propose that solution? – eyllanesc Jul 12 '17 at 05:15
  • @eyllanesc Yes, im open to other options. I MAY want to give color to text down the road so i'd prefer font customization if possible. If not i'll deal with it. – sidnical Jul 12 '17 at 05:17
  • 1
    @sidnical, you can easily use a QStyledItemDelegate with a QTextDocument if you need to provide font customization down the line. – Alex Huszagh Jul 12 '17 at 05:19

1 Answers1

5

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.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241