- PyQt5 Tutorial
- PyQt5 - Home
- PyQt5 - Introduction
- PyQt5 - What’s New
- PyQt5 - Hello World
- PyQt5 - Major Classes
- PyQt5 - Using Qt Designer
- PyQt5 - Signals & Slots
- PyQt5 - Layout Management
- PyQt5 - Basic Widgets
- PyQt5 - QDialog Class
- PyQt5 - QMessageBox
- PyQt5 - Multiple Document Interface
- PyQt5 - Drag & Drop
- PyQt5 - Database Handling
- PyQt5 - Drawing API
- PyQt5 - BrushStyle Constants
- PyQt5 - QClipboard
- PyQt5 - QPixmap Class
- PyQt5 Useful Resources
- PyQt5 - Quick Guide
- PyQt5 - Useful Resources
- PyQt5 - Discussion
PyQt5 - 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.
The following table lists a few important methods of QLineEdit class −
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 PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets 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