9

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?

TrakJohnson
  • 1,755
  • 2
  • 18
  • 31
aste123
  • 1,223
  • 4
  • 20
  • 40
  • 1
    `QTextEdit` can be set to read-only. Did you know that?You've gone to great lengths to say this isn't good enough for your application, but you haven't said what is wrong with using `QTextEdit`. Without more details on why `QTextEdit` is bad (even in read-only mode) I can't suggest anything else. – three_pineapples Aug 05 '14 at 21:59
  • @three_pineapples Thank you very much for letting me know that. I have edited the question. Basically I just need a way to display the text in read only mode with rich formatting (like bold, italics, etc) and different font-sizes and font-faces. Please take a look at the edited question. – aste123 Aug 05 '14 at 22:38

1 Answers1

19

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>')
three_pineapples
  • 11,579
  • 5
  • 38
  • 75
  • +1 for saying Instances of QTextEdit can be made read-only with my_text_edit.setReadOnly(True). What I needed. – umbe1987 Feb 11 '19 at 15:42