Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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: http://pyqt.sourceforge.net/Docs/PyQt4/qtextdocument.html 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?

like image 377
aste123 Avatar asked Aug 05 '14 21:08

aste123


1 Answers

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

my_text_edit.setReadOnly(True)

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: http://qt-project.org/doc/qt-4.8/qtextedit.html#using-qtextedit-as-a-display-widget

but a simple example would be

my_text_edit.textCursor().insertHtml('normal text')
my_text_edit.textCursor().insertHtml('<b>bold text</b>')
like image 110
three_pineapples Avatar answered Nov 19 '22 15:11

three_pineapples