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.
The following table lists a few important methods of QLineEdit class −
Given below are the most commonly used methods of QLineEdit.
setAlignment()
Aligns the text as per alignment constants
Qt.AlignLeft
Qt.AlignRight
Qt.AlignCenter
Qt.AlignJustify
clear()
Erases the contents
setEchoMode()
Controls the appearance of the text inside the box. Echomode values are −
QLineEdit.Normal
QLineEdit.NoEcho
QLineEdit.Password
QLineEdit.PasswordEchoOnEdit
setMaxLength()
Sets the maximum number of characters for input
setReadOnly()
Makes the text box non-editable
setText()
Programmatically sets the text
text()
Retrieves text in the field
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
setInputMask()
Applies mask of combination of characters for input
setFont()
Displays the contents QFont object
QLineEdit object emits the following signals −
Given below are the most commonly used methods of signals.
cursorPositionChanged()
Whenever the cursor moves
editingFinished()
When you press ‘Enter’ or the field loses focus
returnPressed()
When you press ‘Enter’
selectionChanged()
Whenever the selected text changes
textChanged()
As text in the box changes either by input or by programmatic means
textEdited()
Whenever the text is edited
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()
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


    146 Lectures 
     22.5 hours 

 ALAA EID
 Print
 Add Notes
 Bookmark this page