4

I have an application that has some sort of log viewer, that needs to be able to render formatted log records that are inserted to it in real time.

I am writing this in Qt and so far I was using QTextEdit for rendering of the text, which was beautified using some html tags and later inserted either with setHtml or insertHtml.

The problem is however, that these functions are insanely CPU intensive and run for ages, hanging whole application.

A similar question was asked here: QTextEdit.insertHtml() is very slow

Apart it has no really useful answer, other than stating it's really slow, it actually asks a different thing. I don't need to speed up setHtml or insertHtml. I am willing to use entirely different technique if possible.

Is there any mechanism within Qt that would allow for really fast insertions of text? Or maybe even completely different component than QTextEdit?

Is there any way to append new line to QTextEdit which contains rich text (generated in any way) that is really fast?

I also noticed QTextBrowser but it seems to be just an extension of TextEdit, could it be faster?

Community
  • 1
  • 1
Petr
  • 13,747
  • 20
  • 89
  • 144
  • Have you tried to use `QPlainTextEdit` in combination with `QSyntaxHighlighter`? Haven't tried it - just stumbled upon it while searching for a solution (see also: http://stackoverflow.com/a/17466240/4181011) – Simon Kraemer Nov 03 '15 at 16:56
  • At what frequency is append being called? If it's faster than screen rendering (let's say ~60 frames per second), you'd be better off buffering the data and calling append with the buffer, at a slower rate. – TheDarkKnight Nov 03 '15 at 17:07
  • It's about 1 row per second and that is already enough to hang intel i5 with a textedit containng more than 500 lines. (The setHtml() call takes about 8 seconds and is called every 1 second, if I use `insertHtml()` instead, I need to call `toPlainText()` so that I can see how many symbols the text have in order to append to end of document, and that function takes also about 8 seconds, so it's pretty same. – Petr Nov 04 '15 at 11:46

3 Answers3

4

You should give QPlainTextEdit a try. It uses the same technology as QTextEdit but is a lot faster. It is optimized for plain text handling but do not let that fool you, it still has some basic support for formatting using HTML. You can append HTML formatted text with appendHtml().

Daniel Hedberg
  • 5,677
  • 4
  • 36
  • 61
2

In my application, I also need to display a large log of the task, approximately 3500 lines. Some lines of the log should be colored. For this, I used HTML formatting. QTextEdit.setHtml with this amount of text, freezed my GUI.

I replaced QTextEdit with QListWidget, in which QListWidgetItem is created for each line of the log.

It began to work much faster, without friezes.

And I saved colored text, just simple by using the QListWidgetItem.setForeground.

0

[This post][1]

[1]: Performantly appending (rich) text to QTextEdit or QTextBrowser in Qt contains an answer to this problem. The gist: instead of simply appending an HTML snippet, manipulate the underlying document directly.

However, I suggest that if your display really is a list of single lines, you create a QAbstractListModel QAbstractTableModel derived class instead and show it in an item view.

André
  • 570
  • 3
  • 7