QTextBrowser

The QTextBrowser class provides a rich-text browser with hypertext navigation.

This class is read-only, and as an extension of QTextEdit, links to hypertext documents are available.

To use the editable rich-text editor, you must use QTextEdit. Also, to use a text browser without hypertext navigation, use QTextEdit setReadOnly() to make editing impossible.

You can use QLabel to display short rich text.


Example

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLineEdit, QTextBrowser, QPushButton, QVBoxLayout


class MyApp(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):

        self.le = QLineEdit()
        self.le.returnPressed.connect(self.append_text)

        self.tb = QTextBrowser()
        self.tb.setAcceptRichText(True)
        self.tb.setOpenExternalLinks(True)

        self.clear_btn = QPushButton('Clear')
        self.clear_btn.pressed.connect(self.clear_text)

        vbox = QVBoxLayout()
        vbox.addWidget(self.le, 0)
        vbox.addWidget(self.tb, 1)
        vbox.addWidget(self.clear_btn, 2)

        self.setLayout(vbox)

        self.setWindowTitle('QTextBrowser')
        self.setGeometry(300, 300, 300, 300)
        self.show()

    def append_text(self):

        text = self.le.text()
        self.tb.append(text)
        self.le.clear()

    def clear_text(self):

        self.tb.clear()


if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = MyApp()
    sys.exit(app.exec_())

The TimeEdit widget appears on the window.


Description

self.le = QLineEdit()
self.le.returnPressed.connect(self.append_text)

We made a line editor. Press the Enter key to call the append_text method.


self.tb = QTextBrowser()
self.tb.setAcceptRichText(True)
self.tb.setOpenExternalLinks(True)

We created a text browser with QTextBrowser() class.

If you set setAcceptRichText() to True, you can use rich text. (Although it’s True by default and hence unnecessary).

If you set SetOpenExternalLinks() to True, you can connect to an external link.


self.clear_btn = QPushButton('Clear')
self.clear_btn.pressed.connect(self.clear_text)

Clicking clear_btn calls the clear_text method.


def append_text(self):

    text = self.le.text()
    self.tb.append(text)
    self.le.clear()

The append_text method allows text (self.le.text() written in the line editor to be appended to a text browser (self.tb).

When text is added to the text browser, use the clear method to remove text from the line editor.


def clear_text(self):

    self.tb.clear()

When the clear_text method is called, use the clear method to remove text from the text browser (self.tb).


Plain Text
<b>Bold</b>
<i>Italic</i>
<p style="color: red">Red</p>
<p style="font-size: 20px">20px</p>
<a href="https://www.naver.com">Naver</a>

In the Line Editor, type the above text in order and add it to the text browser using the Enter key.


Results

../_images/4_19_qtextbrowser.png

Figure 5-19. Use QTextBrowser to browse text.


Prev/Next