PyQt - QPlainTextEdit



A Plaintext refers to a group of multiple words in a readable format. We can also say unencrypted message, document, or file i.e. used as input to a cryptographic system to produce ciphertext.

Ciphertext is encrypted text transformed from plaintext.

What is QPlainTextEdit?

QPlainTextEdit, a widget in Qt, allows you to create and modify plain text in our Qt apps. Unlike QTextEdit, it's made just for plain text, making it smaller and faster for simple text tasks like writing, editing code, or showing logs. QPlainTextEdit includes features like moving the cursor, selecting text, undoing/redoing actions, and scrolling, so you can use it for many different text-based apps.

QPlainTextEdit provides a user-friendly interface and comprehensive features for developing text editors, input fields, and log viewers. Developers can customize various properties, such as placeholder text, read-only mode, and tab behavior. QPlainTextEdit allows real-time interaction via signals like textChanged() and blockCountChanged(), improving the user's text editing experience. It can be used independently or integrated into larger GUIs, making it a versatile solution for managing plain text editing tasks within Qt applications.

Signals in QPlainTextEdit

  • textChanged() − This emitted when the text changes.
  • blockCountChanged(int newBlockCount) − This emitted when the number of text blocks changes.

Advantages of using QPlainTextEdit

Below is the list of benefits using

  • It easily works with big chunks of simple text.
  • It makes building without fancy text styles easier.
  • QPlainTextEdit adjust how it acts to fit what you want.
  • The users undo and redo stuff they have done.
  • It updates instantly when things change, like when you type.

Example 1

Following example illustrate the code snippet for plaintext in PyQt window.

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QPlainTextEdit

if __name__ == '__main__':
   app = QApplication(sys.argv)

   main_window = QMainWindow()

   # Create text entry box
   textedit_wid = QPlainTextEdit()

   # Set the background color for the text entry box
   textedit_wid.setStyleSheet("background-color: lightgray;")

   # Fill the entire available space
   main_window.setCentralWidget(textedit_wid)

   # Connect the textChanged signal to print the entered text
   textedit_wid.textChanged.connect(
      lambda: print(textedit_wid.document().toPlainText()))

   # Define the initial value of the text
   textedit_wid.document().setPlainText("Please type something here")

   main_window.show()

   # Start the event loop
   sys.exit(app.exec())

Output

The above code produces the following output−

qplaintextedit_ex_one

Example 2

Following is another example demonstrating the code snippet for QPlainTextEdit which builds a widget container. This widget allows the user to enter the input text.

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QPlainTextEdit
from PyQt6.QtCore import QSize

class mainWindow(QMainWindow):
   def __init__(self):
      QMainWindow.__init__(self)

      self.setMinimumSize(QSize(400, 250))
      self.setWindowTitle("QPlainTextEdit")

      # Add text field
      self.b = QPlainTextEdit(self)
      self.b.insertPlainText("Please enter the text here.\n")
      self.b.move(10, 10)
      self.b.resize(500, 200)

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

Output

The above code produces the following output−

qplaintextedit_ex_two
Advertisements