|  | Home · All Classes · Modules

# QLineEdit Class Reference
[QtGui module]

The QLineEdit widget is a one-line text editor. More...

Inherits QWidget.

### Types

- enum EchoMode { Normal, NoEcho, Password, PasswordEchoOnEdit }

### Methods

- __init__ (self, QWidget parent = None)
- __init__ (self, QString, QWidget parent = None)
- Qt.Alignment alignment (self)
- backspace (self)
- changeEvent (self, QEvent)
- clear (self)
- QCompleter completer (self)
- contextMenuEvent (self, QContextMenuEvent)
- copy (self)
- QMenu createStandardContextMenu (self)
- cursorBackward (self, bool, int steps = 1)
- cursorForward (self, bool, int steps = 1)
- int cursorPosition (self)
- int cursorPositionAt (self, QPoint)
- QRect cursorRect (self)
- cursorWordBackward (self, bool)
- cursorWordForward (self, bool)
- cut (self)
- del_ (self)
- deselect (self)
- QString displayText (self)
- bool dragEnabled (self)
- dragEnterEvent (self, QDragEnterEvent)
- dragLeaveEvent (self, QDragLeaveEvent)
- dragMoveEvent (self, QDragMoveEvent)
- dropEvent (self, QDropEvent)
- EchoMode echoMode (self)
- end (self, bool)
- bool event (self, QEvent)
- focusInEvent (self, QFocusEvent)
- focusOutEvent (self, QFocusEvent)
- (int, int, int, int) getTextMargins (self)
- bool hasAcceptableInput (self)
- bool hasFrame (self)
- bool hasSelectedText (self)
- home (self, bool)
- initStyleOption (self, QStyleOptionFrame)
- QString inputMask (self)
- inputMethodEvent (self, QInputMethodEvent)
- QVariant inputMethodQuery (self, Qt.InputMethodQuery)
- insert (self, QString)
- bool isModified (self)
- bool isReadOnly (self)
- bool isRedoAvailable (self)
- bool isUndoAvailable (self)
- keyPressEvent (self, QKeyEvent)
- int maxLength (self)
- QSize minimumSizeHint (self)
- mouseDoubleClickEvent (self, QMouseEvent)
- mouseMoveEvent (self, QMouseEvent)
- mousePressEvent (self, QMouseEvent)
- mouseReleaseEvent (self, QMouseEvent)
- paintEvent (self, QPaintEvent)
- paste (self)
- redo (self)
- selectAll (self)
- QString selectedText (self)
- int selectionStart (self)
- setAlignment (self, Qt.Alignment)
- setCompleter (self, QCompleter)
- setCursorPosition (self, int)
- setDragEnabled (self, bool)
- setEchoMode (self, EchoMode)
- setFrame (self, bool)
- setInputMask (self, QString)
- setMaxLength (self, int)
- setModified (self, bool)
- setReadOnly (self, bool)
- setSelection (self, int, int)
- setText (self, QString)
- setTextMargins (self, int, int, int, int)
- setTextMargins (self, QMargins)
- setValidator (self, QValidator)
- QSize sizeHint (self)
- QString text (self)
- QMargins textMargins (self)
- undo (self)
- QValidator validator (self)

### Qt Signals

- void cursorPositionChanged (int,int)
- void editingFinished ()
- void returnPressed ()
- void selectionChanged ()
- void textChanged (const QString&)
- void textEdited (const QString&)

## Detailed Description

The QLineEdit widget is a one-line text editor.

A line edit allows the user to enter and edit a single line of plain text with a useful collection of 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.

The length of the text can be constrained to maxLength(). The text can be arbitrarily constrained using a validator() or an inputMask(), or both.

A related class is QTextEdit which allows multi-line, rich text editing.

You can change the text with setText() or insert(). The text is retrieved with text(); the displayed text (which may be different, see EchoMode) is retrieved with displayText(). Text can be selected with setSelection() or selectAll(), and the selection can be cut(), copy()ied and paste()d. The text can be aligned with setAlignment().

When the text changes the textChanged() signal is emitted; when the text changes other 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 pressed the returnPressed() signal is emitted.

When editing is finished, either because the line edit lost focus or Return/Enter is pressed the editingFinished() signal is emitted.

Note that if there is a validator set on the line edit, the returnPressed()/editingFinished() signals will only be emitted if the validator returns QValidator.Acceptable.

By default, QLineEdits have a frame as specified by the Windows and Motif style guides; you can turn it off by calling setFrame(false).

The default key bindings are described below. The line edit also provides a context menu (usually invoked by a right mouse click) that presents some of these editing options.

Keypress | 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 | Select 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 key sequence that represents a valid character, will cause the character to be inserted into the line edit.

| A line edit shown in the Macintosh widget style.
| A line edit shown in the Windows XP widget style.
| A line edit shown in the Plastique widget style.

See also QTextEdit, QLabel, QComboBox, GUI Design Handbook: Field, Entry, and Line Edits Example.

## Type Documentation

### QLineEdit.EchoMode

This enum type describes how a line edit should display its contents.

Constant | Value | Description
--- | --- | ---
`QLineEdit.Normal` | `0` | Display characters as they are entered. This is the default.
`QLineEdit.NoEcho` | `1` | Do not display anything. This may be appropriate for passwords where even the length of the password should be kept secret.
`QLineEdit.Password` | `2` | Display asterisks instead of the characters actually entered.
`QLineEdit.PasswordEchoOnEdit` | `3` | Display characters as they are entered while editing otherwise display asterisks.

See also setEchoMode() and echoMode().

## Method Documentation

### QLineEdit.__init__ (self, QWidget parent = None)

The parent argument, if not None, causes self to be owned by Qt instead of PyQt.

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 setText() and setMaxLength().

### QLineEdit.__init__ (self, QString, QWidget parent = None)

The parent argument, if not None, causes self to be owned by Qt instead of PyQt.

Constructs a line edit containing the text contents.

The cursor position is set to the end of the line and the maximum text length to 32767 characters.

The parent and argument is sent to the QWidget constructor.

See also text() and setMaxLength().

### Qt.Alignment QLineEdit.alignment (self)

### QLineEdit.backspace (self)

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 del_().

### QLineEdit.changeEvent (self, QEvent)

Reimplemented from QWidget.changeEvent().

### QLineEdit.clear (self)

Clears the contents of the line edit.

See also setText() and insert().

### QCompleter QLineEdit.completer (self)

Returns the current QCompleter that provides completions.

This function was introduced in Qt 4.2.

See also setCompleter().

### QLineEdit.contextMenuEvent (self, QContextMenuEvent)

Reimplemented from QWidget.contextMenuEvent().

Shows the standard context menu created with createStandardContextMenu().

If you do not want the line edit to have a context menu, you can set its contextMenuPolicy to Qt.NoContextMenu. If you want to customize the context menu, reimplement this function. If you want to extend the standard context menu, reimplement this function, call createStandardContextMenu() and extend the menu returned.

     void LineEdit.contextMenuEvent(QContextMenuEvent *event)
     {
         QMenu *menu = createStandardContextMenu();
         menu->addAction(tr("My Menu Item"));
         //...
         menu->exec(event->globalPos());
         delete menu;
     }

The event parameter is used to obtain the position where the mouse cursor was when the event was generated.

See also setContextMenuPolicy().

### QLineEdit.copy (self)

Copies the selected text to the clipboard, if there is any, and if echoMode() is Normal.

See also cut() and paste().

### QMenu QLineEdit.createStandardContextMenu (self)

This function 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.

### QLineEdit.cursorBackward (self, bool, int steps = 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().

### QLineEdit.cursorForward (self, bool, int steps = 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 cursorBackward().

### int QLineEdit.cursorPosition (self)

### int QLineEdit.cursorPositionAt (self, QPoint)

Returns the cursor position under the point pos.

### QRect QLineEdit.cursorRect (self)

Returns a rectangle that includes the lineedit cursor.

This function was introduced in Qt 4.4.

### QLineEdit.cursorWordBackward (self, bool)

Moves the cursor one word backward. If mark is true, the word is also selected.

See also cursorWordForward().

### QLineEdit.cursorWordForward (self, bool)

Moves the cursor one word forward. If mark is true, the word is also selected.

See also cursorWordBackward().

### QLineEdit.cut (self)

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 copy(), paste(), and setValidator().

### QLineEdit.del_ (self)

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 backspace().

### QLineEdit.deselect (self)

Deselects any selected text.

See also setSelection() and selectAll().

### QString QLineEdit.displayText (self)

### bool QLineEdit.dragEnabled (self)

### QLineEdit.dragEnterEvent (self, QDragEnterEvent)

Reimplemented from QWidget.dragEnterEvent().

### QLineEdit.dragLeaveEvent (self, QDragLeaveEvent)

Reimplemented from QWidget.dragLeaveEvent().

### QLineEdit.dragMoveEvent (self, QDragMoveEvent)

Reimplemented from QWidget.dragMoveEvent().

### QLineEdit.dropEvent (self, QDropEvent)

Reimplemented from QWidget.dropEvent().

### EchoMode QLineEdit.echoMode (self)

### QLineEdit.end (self, 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 home().

### bool QLineEdit.event (self, QEvent)

Reimplemented from QObject.event().

### QLineEdit.focusInEvent (self, QFocusEvent)

Reimplemented from QWidget.focusInEvent().

### QLineEdit.focusOutEvent (self, QFocusEvent)

Reimplemented from QWidget.focusOutEvent().

### (int, int, int, int) QLineEdit.getTextMargins (self)

Returns the widget's text margins for left, top, right, and bottom.

This function was introduced in Qt 4.5.

See also setTextMargins().

### bool QLineEdit.hasAcceptableInput (self)

### bool QLineEdit.hasFrame (self)

### bool QLineEdit.hasSelectedText (self)

### QLineEdit.home (self, 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 end().

### QLineEdit.initStyleOption (self, QStyleOptionFrame)

Initialize option with the values from this QLineEdit. This method is useful for subclasses when they need a QStyleOptionFrame or QStyleOptionFrameV2, but don't want to fill in all the information themselves. This function will check the version of the QStyleOptionFrame and fill in the additional values for a QStyleOptionFrameV2.

See also QStyleOption.initFrom().

### QString QLineEdit.inputMask (self)

### QLineEdit.inputMethodEvent (self, QInputMethodEvent)

Reimplemented from QWidget.inputMethodEvent().

### QVariant QLineEdit.inputMethodQuery (self, Qt.InputMethodQuery)

Reimplemented from QWidget.inputMethodQuery().

### QLineEdit.insert (self, QString)

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.

See also setText() and clear().

### bool QLineEdit.isModified (self)

### bool QLineEdit.isReadOnly (self)

### bool QLineEdit.isRedoAvailable (self)

### bool QLineEdit.isUndoAvailable (self)

### QLineEdit.keyPressEvent (self, QKeyEvent)

Reimplemented from QWidget.keyPressEvent().

Converts the given key press event into a line edit action.

If Return or Enter is pressed and the current text is valid (or can be made valid by the validator), the signal returnPressed() is emitted.

The default key bindings are listed in the class's detailed description.

### int QLineEdit.maxLength (self)

### QSize QLineEdit.minimumSizeHint (self)

Reimplemented from QWidget.minimumSizeHint().

Returns a minimum size for the line edit.

The width returned is enough for at least one character.

### QLineEdit.mouseDoubleClickEvent (self, QMouseEvent)

Reimplemented from QWidget.mouseDoubleClickEvent().

### QLineEdit.mouseMoveEvent (self, QMouseEvent)

Reimplemented from QWidget.mouseMoveEvent().

### QLineEdit.mousePressEvent (self, QMouseEvent)

Reimplemented from QWidget.mousePressEvent().

### QLineEdit.mouseReleaseEvent (self, QMouseEvent)

Reimplemented from QWidget.mouseReleaseEvent().

### QLineEdit.paintEvent (self, QPaintEvent)

Reimplemented from QWidget.paintEvent().

### QLineEdit.paste (self)

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 not be acceptable to the current validator, nothing happens.

See also copy() and cut().

### QLineEdit.redo (self)

Redoes the last operation if redo is available.

### QLineEdit.selectAll (self)

Selects all the text (i.e. highlights it) and moves the cursor to the end. 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 setSelection() and deselect().

### QString QLineEdit.selectedText (self)

### int QLineEdit.selectionStart (self)

selectionStart() returns the index of the first selected character in the line edit or -1 if no text is selected.

See also selectedText().

### QLineEdit.setAlignment (self, Qt.Alignment)

### QLineEdit.setCompleter (self, QCompleter)

Sets this line edit to provide auto completions from the completer, c. The completion mode is set using QCompleter.setCompletionMode().

To use a QCompleter with a QValidator or QLineEdit.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.

If c == 0, setCompleter() removes the current completer, effectively disabling auto completion.

This function was introduced in Qt 4.2.

See also completer() and QCompleter.

### QLineEdit.setCursorPosition (self, int)

### QLineEdit.setDragEnabled (self, bool)

### QLineEdit.setEchoMode (self, EchoMode)

### QLineEdit.setFrame (self, bool)

### QLineEdit.setInputMask (self, QString)

### QLineEdit.setMaxLength (self, int)

### QLineEdit.setModified (self, bool)

### QLineEdit.setReadOnly (self, bool)

### QLineEdit.setSelection (self, int, int)

Selects text from position start and for length characters. Negative lengths are allowed.

See also deselect(), selectAll(), and selectedText().

### QLineEdit.setText (self, QString)

### QLineEdit.setTextMargins (self, int, int, int, int)

Sets the margins around the text inside the frame to have the sizes left, top, right, and bottom.

See also getTextMargins().

This function was introduced in Qt 4.5.

See also textMargins().

### QLineEdit.setTextMargins (self, QMargins)

Sets the margins around the text inside the frame.

See also textMargins().

This function was introduced in Qt 4.6.

### QLineEdit.setValidator (self, QValidator)

Sets this line edit to only accept input that the validator, v, will accept. This allows you to place any arbitrary constraints on the text which may be entered.

If v == 0, setValidator() removes the current input validator. The initial setting is to have no input validator (i.e. any input is accepted up to maxLength()).

See also validator(), QIntValidator, QDoubleValidator, and QRegExpValidator.

### QSize QLineEdit.sizeHint (self)

Reimplemented from QWidget.sizeHint().

Returns a recommended size for the widget.

The width returned, in pixels, is usually enough for about 15 to 20 characters.

### QString QLineEdit.text (self)

### QMargins QLineEdit.textMargins (self)

Returns the widget's text margins.

This function was introduced in Qt 4.6.

See also setTextMargins().

### QLineEdit.undo (self)

Undoes the last operation if undo is available. Deselects any current selection, and updates the selection start to the current cursor position.

### QValidator QLineEdit.validator (self)

Returns a pointer to the current input validator, or 0 if no validator has been set.

See also setValidator().

## Qt Signal Documentation

### void cursorPositionChanged (int,int)

This is the default overload of this signal.

This signal is emitted whenever the cursor moves. The previous position is given by old, and the new position by new.

See also setCursorPosition() and cursorPosition().

### void editingFinished ()

This is the default overload of this signal.

This signal is emitted when the Return or Enter key is pressed or the line edit loses focus. Note that if there is a validator() or inputMask() set on the line edit and enter/return is pressed, the editingFinished() signal will only be emitted if the input follows the inputMask() and the validator() returns QValidator.Acceptable.

### void returnPressed ()

This is the default overload of this signal.

This signal is emitted when the Return or Enter key is pressed. Note that 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 QValidator.Acceptable.

### void selectionChanged ()

This is the default overload of this signal.

This signal is emitted whenever the selection changes.

See also hasSelectedText() and selectedText().

### void textChanged (const QString&)

This is the default overload of this signal.

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().

### void textEdited (const QString&)

This is the default overload of this signal.

This signal is emitted whenever the text is edited. The text argument is the next text.

Unlike textChanged(), this signal is not emitted when the text is changed programmatically, for example, by calling setText().