PyQt QLineEdit

QLineEdit is a widget that allows you to enter and modify a string on a line. By setting echoMode(), you can use it as a 'write-only' area. This is useful when you receive input such as a password.

You can set these modes with the setEchoMode() method, and the inputs and functionality are shown in the table below.

Normal mode is the most commonly used, and is also the default setting. (For example, setEchoMode (QLineEdit.Normal) or setEchoMode (0))

Related course: Create Desktop Apps with Python PyQt5

QLineEdit.

You can edit the text with the setText() or insert() method, and you can import the text entered by the text() method. If the text entered by echoMode is different from the text displayed, you can also import the text displayed by the displayText() method.

You can select text with the setSelection(), selectAll() method, or cut, copy,copy(), paste,and so on through the cut(), copy(), paste() methods. You can also set the alignment of text with the setAlignment() method.

When text changes or the cursor moves, signals such as textChanged() and cursorPositionChanged() occur.

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit

class MyApp(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.lbl = QLabel(self)
        self.lbl.move(60, 40)

        qle = QLineEdit(self)
        qle.move(60, 100)
        qle.textChanged[str].connect(self.onChanged)

        self.setWindowTitle('QLineEdit')
        self.setGeometry(300, 300, 300, 200)
        self.show()

    def onChanged(self, text):
        self.lbl.setText(text)
        self.lbl.adjustSize()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = MyApp()
    sys.exit(app.exec_())

The widget includes one label and one QLineEdit widget.

Text entered and modified in the QLineEdit widget is immediately displayed on the label.

qlineedit pyqt

Related course: Create Desktop Apps with Python PyQt5

Description

qle = QLineEdit(self)

You have created a QLineEdit widget.

qle.textChanged[str].connect(self.onChanged)

If the text in qle changes, call the onChanged() method.

def onChanged(self, text):

self.lbl.setText(text)
self.lbl.adjustSize()

Within the onChanged() method, set the input 'text' to the text of the label widget (lbl).

The AdjustSize() method also allows you to adjust the length of the label according to the length of the text.