
- PyQt - Home
- PyQt - Introduction
- PyQt - Environment
- PyQt - Hello World
- PyQt - Major Classes
- PyQt - Using Qt Designer
- PyQt - Meta Objects
- PyQt Signals & Slots
- PyQt - Signals and Slots
- PyQt - Support and Signals
- PyQt - Unbound and Bound Signals
- PyQt - New Signals with PyQtSignal
- PyQt - Connecting, Disconnecting, & Emitting Signals
- PyQt - Slot decorator
- PyQt - Slot Connection
- PyQt Layouts
- PyQt - Layout Management
- PyQt - QBoxLayout
- PyQt - QGridLayout
- PyQt - QFormLayout
- PyQt - QHBoxLayout
- PyQt - QVBoxLayout
- PyQt - QStackedLayout
- PyQt - QGraphicsGridLayout
- PyQt - QGraphicsAnchorLayout
- PyQt - QGraphicsLayout
- PyQt - QGraphicsLinearLayout
- PyQt Basic Widgets
- PyQt - Basic Widgets
- PyQt - Qlabel Widget
- PyQt - QLineEdit Widget
- PyQt - QPushButton Widget
- PyQt - QRadioButton Widget
- PyQt - QCheckBox Widget
- PyQt - QComboBox Widget
- PyQt - QSpinBox Widget
- PyQt - QMessageBox
- PyQt - QDialogButtonBox Widget
- PyQt - QFontComboBox Widget
- PyQt - QDoubleSpinBox Widget
- PyQt - QToolBox Widget
- PyQt - QMenuBar, QMenu & Qaction Widgets
- PyQt - QToolTip
- PyQt - QInputDialog Widget
- PyQt - QFontDialog Widget
- PyQt - QDialog Widget
- PyQt - QFileDialog Widget
- PyQt - QTab Widget
- PyQt - QSplitter Widget
- PyQt - QDock Widget
- PyQt - QStatusBar Widget
- PyQt - QTabBar
- PyQt - QList Widget
- PyQt - QScrollBar Widget
- PyQt - QProgressBar
- PyQt - QCalendar Widget
- PyQt - QMessageBox Widget
- PyQt - QPlainTextEdit
- PyQt - QDateEdit
- PyQt - QDateTimeEdit
- PyQt - QTimeEdit
- PyQt - QTextEdit
- PyQt - QTextBrowser
- PyQt - QScrollArea
- PyQt - Drag and Drop
- PyQt - Multiple Document Interface
- PyQt - QDialog Class
- PyQt Views
- PyQt - QColumnView
- PyQt - QTableView
- PyQt Drawing API
- PyQt - Drawing API
- PyQt - Drawing a Line
- PyQt - Drawing a Rectangle
- PyQt - Drawing a Triangle
- PyQt - Drawing a Circle
- PyQt - Drawing a Ellipse
- PyQt - Drawing a Polygon
- PyQt - Geometric Transformation
- PyQt - Drawing Effect
- PyQt Groups
- PyQt - QButtonGroup
- PyQt - QGroupBox
- PyQt Effects
- PyQt - Effects
- PyQt - Opacity Effect
- PyQt - QGraphicsBlur Effect
- PyQt - QGraphicsColorize Effect
- PyQt - QGraphicsDropShadow Effect
- PyQt Events
- PyQt - Event Handling
- PyQt - Drag & Drop Events
- PyQt - File Open Event
- PyQt - Action Event
- PyQt - Hide Event
- PyQt - Resize Event
- PyQt Database
- PyQt - Database Handling
- PyQt Essentials
- PyQt - BrushStyle Constants
- PyQt - QClipboard
- PyQt - QPixmap Class
- PyQt Useful Resources
- PyQt - Quick Guide
- PyQt - Useful Resources
- PyQt - Discussion
PyQt - QLineEdit Widget
QLineEdit object is the most commonly used input field. It provides a box in which one line of text can be entered. In order to enter multi-line text, QTextEdit object is required.
What is QlineEdit Widget
The PyQt QLineEdit widget is an essential component in GUI applications that is mainly used for single-line text entry purposes. It is very commonly used in data-entry forms and it also seamlessly integrate with other PyQt widgets, such as QLabel, to create interactive and user-friendly interfaces.
Creating a QLineEdit Widget
To create a Qline edit widget, follow the below steps −
Step 1: Importing QLineEdit − Import the QLineEdit class from the PyQt6.QtWidgets module.
from PyQt6.QtWidgets import QLineEdit
Step 2: Instantiation − Create a new instance of the QLineEdit class. This can be done with various arguments.
- No arguments: Creates an empty QLineEdit.
- With a parent widget: Specifies the parent widget for the QLineEdit.
- With a default string value: Sets an initial text for the QLineEdit.
line_edit = QLineEdit('Default Value', parent_widget)
The following table lists a few important methods of QLineEdit class −
Given below are the most commonly used methods of QLineEdit.
Sr.No. | Methods & Description |
---|---|
1 |
setAlignment() Aligns the text as per alignment constants Qt.AlignLeft Qt.AlignRight Qt.AlignCenter Qt.AlignJustify |
2 |
clear() Erases the contents |
3 |
setEchoMode() Controls the appearance of the text inside the box. Echomode values are − QLineEdit.Normal QLineEdit.NoEcho QLineEdit.Password QLineEdit.PasswordEchoOnEdit |
4 |
setMaxLength() Sets the maximum number of characters for input |
5 |
setReadOnly() Makes the text box non-editable |
6 |
setText() Programmatically sets the text |
7 |
text() Retrieves text in the field |
8 |
setValidator() Sets the validation rules. Available validators are QIntValidator − Restricts input to integer QDoubleValidator − Fraction part of number limited to specified decimals QRegexpValidator − Checks input against a Regex expression |
9 |
setInputMask() Applies mask of combination of characters for input |
10 |
setFont() Displays the contents QFont object |
QLineEdit object emits the following signals −
Given below are the most commonly used methods of signals.
Sr.No. | Methods & Description |
---|---|
1 |
cursorPositionChanged() Whenever the cursor moves |
2 |
editingFinished() When you press Enter or the field loses focus |
3 |
returnPressed() When you press Enter |
4 |
selectionChanged() Whenever the selected text changes |
5 |
textChanged() As text in the box changes either by input or by programmatic means |
6 |
textEdited() Whenever the text is edited |
Example
QLineEdit objects in this example demonstrate use of some of these methods.
First field e1 shows text using a custom font, in right alignment and allows integer input. Second field restricts input to a number with 2 digits after decimal point. An input mask for entering the phone number is applied on the third field. textChanged() signal on the field e4 is connected to textchanged() slot method.
Contents of e5 field are echoed in password form as its EchoMode property is set to Password. Its editingfinished() signal is connected to presenter() method. So, once the user presses the Enter key, the function will be executed. The field e6 shows a default text, which cannot be edited as it is set to read only.
import sys from PyQt4.QtCore import * from PyQt4.QtGui import * def window(): app = QApplication(sys.argv) win = QWidget() e1 = QLineEdit() e1.setValidator(QIntValidator()) e1.setMaxLength(4) e1.setAlignment(Qt.AlignRight) e1.setFont(QFont("Arial",20)) e2 = QLineEdit() e2.setValidator(QDoubleValidator(0.99,99.99,2)) flo = QFormLayout() flo.addRow("integer validator", e1) flo.addRow("Double validator",e2) e3 = QLineEdit() e3.setInputMask('+99_9999_999999') flo.addRow("Input Mask",e3) e4 = QLineEdit() e4.textChanged.connect(textchanged) flo.addRow("Text changed",e4) e5 = QLineEdit() e5.setEchoMode(QLineEdit.Password) flo.addRow("Password",e5) e6 = QLineEdit("Hello Python") e6.setReadOnly(True) flo.addRow("Read Only",e6) e5.editingFinished.connect(enterPress) win.setLayout(flo) win.setWindowTitle("PyQt") win.show() sys.exit(app.exec_()) def textchanged(text): print "contents of text box: "+text def enterPress(): print "edited" if __name__ == '__main__': window()
Output
The above code produces the following output −

contents of text box: h contents of text box: he contents of text box: hel contents of text box: hell contents of text box: hello editing finished
Using QLineEdit for Password Entry
In the below example we will use the QlineEdit widget to create password entry field.In this example we will create a PyQt application window with a single-line QLineEdit widget configured for password input. When the code is executed, it displays a window titled "PyQt QLineEdit Widget" with a password entry field. The echoMode property of the QLineEdit widget is set to Password which ensures that the input characters are masked for security.
Example
Following is the illustration of QLineEdit for Password Entry.
import sys from PyQt6.QtWidgets import QApplication, QWidget, QLineEdit, QVBoxLayout class MainWindow(QWidget): def __init__(self): super().__init__() self.setWindowTitle('PyQt QLineEdit Widget') self.setGeometry(100, 100, 320, 210) password = QLineEdit(echoMode=QLineEdit.EchoMode.Password) layout = QVBoxLayout() layout.addWidget(password) self.setLayout(layout) self.show() if __name__ == '__main__': app = QApplication(sys.argv) window = MainWindow() sys.exit(app.exec())
Output
The above code produces the following output −

Using Auto-Complete Feature with QLineEdit
Here, we will create a auto complete feature using Qline Edit widget. In this example we will initialize a PyQt application window featuring a single-line QLineEdit widget. The QLineEdit is implemented with an auto-complete feature which uses QCompleter that gives suggestions based on a predefined list of common fruits. When the code is executed, the window titled "PyQt QLineEdit Widget" appears with the QLineEdit field, providing auto-completion functionality for fruit names as the user types.
Example
Following is the illustration of Auto-Complete Features with QlineEdit.
import sys from PyQt6.QtWidgets import QApplication, QWidget, QLineEdit, QVBoxLayout, QCompleter class MainWindow(QWidget): def __init__(self): super().__init__() self.setWindowTitle('PyQt QLineEdit Widget') self.setGeometry(100, 100, 320, 210) common_fruits = QCompleter([ 'Apple', 'Apricot', 'Banana', 'Carambola', 'Olive', 'Oranges', 'Papaya', 'Peach', 'Pineapple', 'Pomegranate', 'Rambutan', 'Ramphal', 'Raspberries', 'Rose apple', 'Starfruit', 'Strawberries', 'Water apple' ]) fruit = QLineEdit() fruit.setCompleter(common_fruits) layout = QVBoxLayout() layout.addWidget(fruit) self.setLayout(layout) self.show() if __name__ == '__main__': app = QApplication(sys.argv) window = MainWindow() sys.exit(app.exec())
Output
The above code produces the following output −
