QLineEdit¶
- PyQt6.QtWidgets.QLineEdit
Inherits from QWidget.
Description¶
The QLineEdit widget is a one-line text editor.

A line edit allows users to enter and edit a single line of plain text with useful editing functions, including undo and redo, cut and paste, and drag and drop.
By changing the echoMode() of a line edit, it can also be used as a write-only field for inputs such as passwords.
QTextEdit is a related class that allows multi-line, rich text editing.
Constraining Text¶
Use maxLength() to define the maximum permitted length of a text. You can use a inputMask() and setValidator() to further constrain the text content.
Editing Text¶
You can change the text with setText() or insert(). Use text() to retrieve the text and displayText() to retrieve the displayed text (which may be different, see EchoMode). You can select the text with setSelection() or selectAll(), and you can cut(), copy(), and paste() the selection. To align the text, use setAlignment().
When the text changes, the textChanged signal is emitted. When the text changes in some other way than by calling setText(), the textEdited signal is emitted. When the cursor is moved, the cursorPositionChanged signal is emitted. And when the Return or Enter key is selected, the returnPressed signal is emitted.
When text editing is finished, either because the line edit lost focus or Return/Enter was selected, the editingFinished signal is emitted.
If the line edit focus is lost without any text changes, the editingFinished signal won’t be emitted.
If there is a validator set on the line edit, the returnPressed/editingFinished signals will only be emitted if the validator returns Acceptable.
For more information on the many ways that QLineEdit can be used, see Line Edits Example, which also provides a selection of line edit examples that show the effects of various properties and validators on the input and output supplied by the user.
Setting a Frame¶
By default, QLineEdits have a frame as specified in the platform style guides. You can turn the frame off by calling setFrame()(false).
Default Key Bindings¶
The table below describes the default key bindings.
Note: The line edit also provides a context menu (usually invoked by a right-click) that presents some of the editing options listed below.
Keystroke |
Action |
---|---|
Left Arrow |
Moves the cursor one character to the left. |
Shift+Left Arrow |
Moves and selects text one character to the left. |
Right Arrow |
Moves the cursor one character to the right. |
Shift+Right Arrow |
Moves and selects text one character to the right. |
Home |
Moves the cursor to the beginning of the line. |
End |
Moves the cursor to the end of the line. |
Backspace |
Deletes the character to the left of the cursor. |
Ctrl+Backspace |
Deletes the word to the left of the cursor. |
Delete |
Deletes the character to the right of the cursor. |
Ctrl+Delete |
Deletes the word to the right of the cursor. |
Ctrl+A |
Selects all. |
Ctrl+C |
Copies the selected text to the clipboard. |
Ctrl+Insert |
Copies the selected text to the clipboard. |
Ctrl+K |
Deletes to the end of the line. |
Ctrl+V |
Pastes the clipboard text into line edit. |
Shift+Insert |
Pastes the clipboard text into line edit. |
Ctrl+X |
Deletes the selected text and copies it to the clipboard. |
Shift+Delete |
Deletes the selected text and copies it to the clipboard. |
Ctrl+Z |
Undoes the last operation. |
Ctrl+Y |
Redoes the last undone operation. |
Any other keystroke that represents a valid character, will cause the character to be inserted into the line edit.
See also
Enums¶
- ActionPosition
This enum type describes how a line edit should display the action widgets to be added.
See also
addAction(), removeAction(), layoutDirection().
Member
Value
Description
LeadingPosition 0
The widget is displayed to the left of the text when using layout direction
Qt::LeftToRight
or to the right when usingQt::RightToLeft
, respectively.TrailingPosition 1
The widget is displayed to the right of the text when using layout direction
Qt::LeftToRight
or to the left when usingQt::RightToLeft
, respectively.
- EchoMode
This enum type describes how a line edit should display its contents.
See also
Member
Value
Description
NoEcho 1
Do not display anything. This may be appropriate for passwords where even the length of the password should be kept secret.
Normal 0
Display characters as they are entered. This is the default.
Password 2
Display platform-dependent password mask characters instead of the characters actually entered.
PasswordEchoOnEdit 3
Display characters only while they are entered. Otherwise, display characters as with
Password
.
Methods¶
- __init__(parent: QWidget = None)
Constructs a line edit with no text.
The maximum text length is set to 32767 characters.
The parent argument is sent to the QWidget constructor.
See also
- __init__(Optional[str], parent: QWidget = None)
Constructs a line edit containing the text contents as a child of parent.
The cursor position is set to the end of the line and the maximum text length to 32767 characters.
See also
- addAction(QAction)
TODO
- addAction(QAction, ActionPosition)
Adds the action to the list of actions at the position.
- addAction(QIcon, ActionPosition) QAction
This is an overloaded function.
Creates a new action with the given icon at the position.
- alignment() AlignmentFlag
See also
- backspace()
If no text is selected, deletes the character to the left of the text cursor, and moves the cursor one position to the left. If any text is selected, the cursor is moved to the beginning of the selected text, and the selected text is deleted.
See also
- changeEvent(QEvent)
TODO
- clear()
Clears the contents of the line edit.
- completer() QCompleter
Returns the current QCompleter that provides completions.
See also
- contextMenuEvent(QContextMenuEvent)
TODO
- copy()
Copies the selected text to the clipboard, if there is any, and if echoMode() is Normal.
- createStandardContextMenu() QMenu
Creates the standard context menu, which is shown when the user clicks on the line edit with the right mouse button. It is called from the default contextMenuEvent() handler. The popup menu’s ownership is transferred to the caller.
- cursorBackward(bool, steps: int = 1)
Moves the cursor back steps characters. If mark is true each character moved over is added to the selection; if mark is false the selection is cleared.
See also
- cursorForward(bool, steps: int = 1)
Moves the cursor forward steps characters. If mark is true each character moved over is added to the selection; if mark is false the selection is cleared.
See also
- cursorMoveStyle() CursorMoveStyle
See also
- cursorPosition() int
See also
- cursorPositionAt(QPoint) int
Returns the cursor position under the point pos.
- cursorRect() QRect
Returns a rectangle that includes the lineedit cursor.
- cursorWordBackward(bool)
Moves the cursor one word backward. If mark is true, the word is also selected.
See also
- cursorWordForward(bool)
Moves the cursor one word forward. If mark is true, the word is also selected.
See also
- cut()
Copies the selected text to the clipboard and deletes it, if there is any, and if echoMode() is Normal.
If the current validator disallows deleting the selected text, cut() will copy without deleting.
See also
- del_()
If no text is selected, deletes the character to the right of the text cursor. If any text is selected, the cursor is moved to the beginning of the selected text, and the selected text is deleted.
See also
- deselect()
Deselects any selected text.
See also
- displayText() str
TODO
- dragEnabled() bool
See also
- dragEnterEvent(QDragEnterEvent)
TODO
- dragLeaveEvent(QDragLeaveEvent)
TODO
- dragMoveEvent(QDragMoveEvent)
TODO
- dropEvent(QDropEvent)
TODO
- echoMode() EchoMode
See also
- end(bool)
Moves the text cursor to the end of the line unless it is already there. If mark is true, text is selected towards the last position; otherwise, any selected text is unselected if the cursor is moved.
See also
- event(QEvent) bool
TODO
- focusInEvent(QFocusEvent)
TODO
- focusOutEvent(QFocusEvent)
TODO
- hasAcceptableInput() bool
TODO
- hasFrame() bool
TODO
- hasSelectedText() bool
TODO
- home(bool)
Moves the text cursor to the beginning of the line unless it is already there. If mark is true, text is selected towards the first position; otherwise, any selected text is unselected if the cursor is moved.
See also
- initStyleOption(QStyleOptionFrame)
Initialize option with the values from this QLineEdit. This method is useful for subclasses when they need a QStyleOptionFrame, but don’t want to fill in all the information themselves.
See also
- inputMask() str
See also
- inputMethodEvent(QInputMethodEvent)
TODO
- inputMethodQuery(InputMethodQuery) Any
TODO
- inputMethodQuery(InputMethodQuery, Any) Any
TODO
- insert(Optional[str])
Deletes any selected text, inserts newText, and validates the result. If it is valid, it sets it as the new contents of the line edit.
- isClearButtonEnabled() bool
TODO
- isModified() bool
TODO
- isReadOnly() bool
TODO
- isRedoAvailable() bool
TODO
- isUndoAvailable() bool
TODO
- keyPressEvent(QKeyEvent)
TODO
- keyReleaseEvent(QKeyEvent)
TODO
- maxLength() int
See also
- minimumSizeHint() QSize
TODO
- mouseDoubleClickEvent(QMouseEvent)
TODO
- mouseMoveEvent(QMouseEvent)
TODO
- mousePressEvent(QMouseEvent)
TODO
- mouseReleaseEvent(QMouseEvent)
TODO
- paintEvent(QPaintEvent)
TODO
- paste()
Inserts the clipboard’s text at the cursor position, deleting any selected text, providing the line edit is not read-only.
If the end result would be invalid to the current setValidator(), nothing happens.
- placeholderText() str
See also
- redo()
Redoes the last operation if redo is available.
- selectAll()
Selects all the text (highlights it) and moves the cursor to the end.
Note: This is useful when a default value has been inserted because if the user types before clicking on the widget, the selected text will be deleted.
See also
- selectedText() str
TODO
- selectionEnd() int
Returns the index of the character directly after the selection in the line edit (or -1 if no text is selected).
See also
- selectionLength() int
Returns the length of the selection.
See also
- selectionStart() int
Returns the index of the first selected character in the line edit (or -1 if no text is selected).
See also
- setAlignment(AlignmentFlag)
See also
- setClearButtonEnabled(bool)
See also
- setCompleter(QCompleter)
Sets this line edit to provide auto completions from the completer, c. The completion mode is set using setCompletionMode().
To use a QCompleter with a QValidator or inputMask(), you need to ensure that the model provided to QCompleter contains valid entries. You can use the QSortFilterProxyModel to ensure that the QCompleter’s model contains only valid entries.
To remove the completer and disable auto-completion, pass a
nullptr
.See also
- setCursorMoveStyle(CursorMoveStyle)
See also
- setCursorPosition(int)
See also
- setDragEnabled(bool)
See also
- setEchoMode(EchoMode)
See also
- setFrame(bool)
See also
- setInputMask(Optional[str])
See also
- setMaxLength(int)
See also
- setModified(bool)
See also
- setPlaceholderText(Optional[str])
See also
- setReadOnly(bool)
See also
- setSelection(int, int)
Selects text from position start and for length characters. Negative lengths are allowed.
See also
- setText(Optional[str])
See also
- setTextMargins(QMargins)
Sets the margins around the text inside the frame.
See also
- setTextMargins(int, int, int, int)
Sets the margins around the text inside the frame to have the sizes left, top, right, and bottom.
See also
- setValidator(QValidator)
Sets the validator for values of line edit to v.
The line edit’s returnPressed and editingFinished signals will only be emitted if v validates the line edit’s content as Acceptable. The user may change the content to any Intermediate value during editing, but will be prevented from editing the text to a value that v validates as Invalid.
This allows you to constrain the text that will be stored when editing is done while leaving users with enough freedom to edit the text from one valid state to another.
To remove the current input validator, pass
nullptr
. The initial setting is to have no input validator (any input is accepted up to maxLength()).
- sizeHint() QSize
TODO
- text() str
See also
- textMargins() QMargins
Returns the widget’s text margins.
See also
- timerEvent(QTimerEvent)
TODO
- undo()
Undoes the last operation if undo is available. Deselects any current selection, and updates the selection start to the current cursor position.
- validator() QValidator
Returns a pointer to the current input validator, or
nullptr
if no validator has been set.See also
Signals¶
- cursorPositionChanged(int, int)
This signal is emitted whenever the cursor moves. The previous position is given by oldPos, and the new position by newPos.
See also
- editingFinished()
This signal is emitted when the Return or Enter key is used, or if the line edit loses focus and its contents have changed since the last time this signal was emitted.
Note: If there is a validator() or inputMask() set on the line edit and enter/return is used, the editingFinished() signal will only be emitted if the input follows the inputMask() and the validator() returns Acceptable.
- inputRejected()
This signal is emitted when the user uses a key that is not considered to be valid input. For example, if using a key results in a validator’s validate() call to return Invalid. Another case is when trying to enter more characters beyond the maximum length of the line edit.
Note: This signal will still be emitted when only a part of the text is accepted. For example, if there is a maximum length set and the clipboard text is longer than the maximum length when it is pasted.
- returnPressed()
This signal is emitted when the Return or Enter key is used.
Note: If there is a validator() or inputMask() set on the line edit, the returnPressed() signal will only be emitted if the input follows the inputMask() and the validator() returns Acceptable.
- selectionChanged()
This signal is emitted whenever the selection changes.
See also
- textChanged(Optional[str])
This signal is emitted whenever the text changes. The text argument is the new text.
Unlike textEdited, this signal is also emitted when the text is changed programmatically, for example, by calling setText().
- textEdited(Optional[str])
This signal is emitted whenever the text is edited. The text argument is the new text.
Unlike textChanged, this signal is not emitted when the text is changed programmatically, for example, by calling setText().