PyQt - QTextEdit



In GUI development, building a text editor plays a crucial role for every computer user. While developing any software it always requires an editor to write the code or make documentation. In this tutorial, we learn the concept of QTextEdit to build a simple text editor in PyQt.

What is QTextEdit?

The QTextEdit is a class that can be used to display both plain and rich text.

QTextEdit is a text editing widget that works with paragraphs and characters. In its default setting, a new line represents a new paragraph. Each paragraph is a formatted string that is word-wrapped to fit into the widget's width. The document can have zero or more paragraphs, and each paragraph has its alignment. The hard line breaks separate paragraphs. Each character within a paragraph has unique attributes such as font and color.

We use QLabel to display the small set of rich text. In Qt framework, rich text are highly designed for online application tool and provide multi-featured web browser widget.

Note that the class QLineEdit provide users to build a widgets that support single line editing text but QTextEdit is specially designed for handling larger documentation.

Methods of QTextEdit class in PyQt

Below are the list of methods that are very useful to build a text editor using PyQt −

  • QTextEdit() − This method reference from the class QTextEdit and create the text edit to the PyQt window.
  • addWidget() − This method adds a widget at specified row and column
  • setLayout() − This method is used to assign the layout object to the parent widget's layout.
  • toPlainText() − This method is used to handle or optimize the large document and have quick response to user input.
  • setPlainText() − This method is used to replace the existing text.
  • setMinimumWidth() − It can set the minimum size of the window.

Example 1

Following example demonstrate the code snippet for simple GUI with two text edit widgets and a button. Here, the first widget allows users to enter any number and when clicking on the button widget it adds 20 to the existing number and generates the output in the second widget.

import sys
from PyQt6.QtWidgets import QApplication, QWidget, QTextEdit, QVBoxLayout, QPushButton
class TextEditDemo(QWidget):
   def __init__(self, parent=None):
      super().__init__(parent)

      # Setting the title of the GUI
      self.setWindowTitle("QTextEdit")

      # Setting the size of the window
      self.resize(300, 270)

      # Creating the first textedit
      self.textEdit1 = QTextEdit()

      # Creating the second textedit
      self.textEdit2 = QTextEdit()

      # Creating the button
      self.btnPress1 = QPushButton("Click Here")

      # Creating a vertical box layout
      layout = QVBoxLayout()

      # Adding the first textedit into the layout
      layout.addWidget(self.textEdit1)

      # Adding the second textedit into the layout
      layout.addWidget(self.textEdit2)

      # Adding the button into the layout
      layout.addWidget(self.btnPress1)

      self.setLayout(layout)

      # Calling the function when the button is clicked
      self.btnPress1.clicked.connect(self.btnPress1_Clicked)

   def btnPress1_Clicked(self):
      result = int(self.textEdit1.toPlainText()) +  20

      # Displaying the result in the second textEdit.
      self.textEdit2.setPlainText(str(result))

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

Output

The above code produces the following output −

qtextedit_ex_one

Example 2

Following example demonstrate the code snippet for text editor within a single widget container.

import sys
from PyQt6.QtWidgets import QApplication, QWidget, QTextEdit, QFormLayout
from PyQt6.QtCore import Qt
class MainWindow(QWidget):
   def __init__(self, *args, **kwargs):
      super().__init__(*args, **kwargs)

      self.setWindowTitle('QTexEdit')
      self.setMinimumWidth(200)

      layout = QFormLayout()
      self.setLayout(layout)
      text_edit = QTextEdit(self)
      layout.addRow(text_edit)

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

Output

The above code produces the following output−

qtextedit_ex_two
Advertisements