Python – How to display some non editable text in rich format in GUI created by PyQt4


I have some python code that generates some information that I want to be able to print or display in a window.

The whole window will be used to display the text with rich format (bold, italics, colored fonts, various font sizes, etc.). The text should also be read only. Also the cursor should not be visible. Just like in a web-browser.

Which PyQt class should I use for this? If this can be done using QTextEdit, please let me know how to make it read only and apply the various kinds of formatting to the text.If any other PyQt class is more suitable for this, please let me know.

UPDATE: I found this class:
It says

QTextDocument is a container for structured rich text documents, providing support for styled text and various types of document elements, such as lists, tables, frames, and images. They can be created for use in a QTextEdit, or used independently.

Is there an advantage of using QTextDocument class instead of the QTextEdit directly?

Best Solution

You probably still want to use QTextEdit. Instances of QTextEdit can be made read-only by the following:


You can then insert/append text using QTextCursors or using setHtml() which allows you to set the entire contents of the text edit. The formatting syntax is basic HTML, like <b> etc. you can read a bunch more about that here:

but a simple example would be

my_text_edit.textCursor().insertHtml('normal text')
my_text_edit.textCursor().insertHtml('<b>bold text</b>')
Related Question