PyQt - QTextBrowser



The term text browser is a web browser which rendered the text of the web pages. The text browser also ignored the graphical content.

In PyQt, QTextBrowser is a class which extend from another class named QTextEdit and adding the navigation functionality.

Users can follow links in hypertext documents. If you are looking to provide your users with an editable rich text editor, you can use QTextEdit. On the other hand, if you require a text browser without hypertext navigation, you can also use QTextEdit and use QTextEdit::setReadOnly() function to disable editing. Thus, if you only need to display a small piece of rich text, you can use QLabel.

The QTextBrowser class use the method setPlainText() to provide source text to the browser window.

Methods used in QTextBrowser

Following is the list of methods that can be used to build a text browser in PyQt −

  • QTextBrowser() − This is a constructor method used to create a instance of a class QTextBrowser that provide a rich text browser and hypertext navigation.
  • addwidget() − This add the QTextBrowser to the layout.
  • setPlainText() − This allows user to write the text for browser window.
  • setOpenExternalLinks(True) − This method enable the QTextBrowser to open the external link.
  • loadText() − This method load the content to the text browser.

Example 1

In the following example, we display the text browser using the class QTextBrowser and its method. The group of text uses the method setPlainText() and addWidget() to call the main function of the program and display the output.

import sys
from PyQt6.QtWidgets import QApplication, QWidget, QTextBrowser, QVBoxLayout
class TextBrowser(QWidget):
   def __init__(self):
      super().__init__()
      self.setWindowTitle("QTextBrowser")
      # Set window size
      self.setGeometry(100, 100, 400, 300)  

      # Create a vertical layout
      layout = QVBoxLayout(self)

      # Create a QTextBrowser
      self.text_browser = QTextBrowser(self)
      # Enable opening external links
      self.text_browser.setOpenExternalLinks(True)  

      # Set some sample text (you can replace this with your own content)
      self.text_browser.setPlainText("Welcome to the QTextBrowser demo!\n\n"
            "This widget allows you to display rich text, "
            "including hyperlinks and formatting.")

      # Add the QTextBrowser to the layout
      layout.addWidget(self.text_browser)

if __name__ == "__main__":
   app = QApplication(sys.argv)
   window = TextBrowser()
   window.show()
   sys.exit(app.exec())

Output

The above code produces the following output−

qtextbrowser_ex_one

Example 2

Following is another example to illustrate the code snippet for text browser using QTextBrowser class and it's relevant methods.

import sys
from PyQt6.QtWidgets import QApplication, QWidget, QTextBrowser, QVBoxLayout

class TextBrowserApp(QWidget):
   def __init__(self):
      super().__init__()
      self.initUI()

   def initUI(self):
      self.setWindowTitle('Text Browser')
      self.setGeometry(100, 100, 400, 300)

      layout = QVBoxLayout()
      self.text_browser = QTextBrowser()
      layout.addWidget(self.text_browser)

      self.setLayout(layout)

      # Load initial text
      self.loadText()

   def loadText(self):
      # Load text into the text browser
      self.text_browser.setPlainText("Welcome to the Text Browser!")

if __name__ == '__main__':
   app = QApplication(sys.argv)
   window = TextBrowserApp()
   window.show()
   sys.exit(app.exec())

Output

The above code produces the following result−

qtextbrowser_ex_two

Input text using QTextBrowser

Here, we have text widgets in Pyqt window that allows user to enter the text. Building of input text in QTextBrowser uses various method of PyQt −

  • QLineEdit() − QLineEdit widget is a one-line text editor.
  • QPushButton() − This method create a functional button while pressing on it.
  • QVBoxLayout() − The QVBoxLayout is a class that constructs the layout container vertically.
  • append() − Add the text to end.
  • clear() − This method reset all the text from the browser window.

Example

In this example, we have two sets of vertical widget containers. The first container allows the user to enter the single-line text and when pressed to enter to keyboard it simply moves to the second container.

import sys
from PyQt6.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.tab = QTextBrowser()
      self.tab.setAcceptRichText(True)
      self.tab.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.tab, 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.tab.append(text)
      self.le.clear()

   def clear_text(self):
      self.tab.clear()

if __name__ == '__main__':

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

Output

The above code produces the following output−

qtextbrowser_ex_three
Advertisements