QLineEdit Class

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

Header: #include <QLineEdit>
qmake: QT += widgets
Inherits: QWidget

Public Types

enum ActionPosition { LeadingPosition, TrailingPosition }
enum EchoMode { Normal, NoEcho, Password, PasswordEchoOnEdit }

Properties

Public Functions

Qt::Alignment alignment() const
Qt::CursorMoveStyle cursorMoveStyle() const
int cursorPosition() const
QString displayText() const
bool dragEnabled() const
QLineEdit::EchoMode echoMode() const
bool hasAcceptableInput() const
bool hasFrame() const
bool hasSelectedText() const
QString inputMask() const
bool isClearButtonEnabled() const
bool isModified() const
bool isReadOnly() const
bool isRedoAvailable() const
bool isUndoAvailable() const
int maxLength() const
QString placeholderText() const
QString selectedText() const
void setAlignment(Qt::Alignment flag)
void setClearButtonEnabled(bool enable)
void setCursorMoveStyle(Qt::CursorMoveStyle style)
void setCursorPosition(int)
void setDragEnabled(bool b)
void setEchoMode(QLineEdit::EchoMode)
void setFrame(bool)
void setInputMask(const QString &inputMask)
void setMaxLength(int)
void setModified(bool)
void setPlaceholderText(const QString &)
void setReadOnly(bool)
QString text() const

Public Slots

void setText(const QString &)

Signals

void textChanged(const QString &)

Detailed Description

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

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. When switching between a validator and an input mask on the same line edit, it is best to clear the validator or input mask to prevent undefined behavior.

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 platform 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.

KeypressAction
Left ArrowMoves the cursor one character to the left.
Shift+Left ArrowMoves and selects text one character to the left.
Right ArrowMoves the cursor one character to the right.
Shift+Right ArrowMoves and selects text one character to the right.
HomeMoves the cursor to the beginning of the line.
EndMoves the cursor to the end of the line.
BackspaceDeletes the character to the left of the cursor.
Ctrl+BackspaceDeletes the word to the left of the cursor.
DeleteDeletes the character to the right of the cursor.
Ctrl+DeleteDeletes the word to the right of the cursor.
Ctrl+ASelect all.
Ctrl+CCopies the selected text to the clipboard.
Ctrl+InsertCopies the selected text to the clipboard.
Ctrl+KDeletes to the end of the line.
Ctrl+VPastes the clipboard text into line edit.
Shift+InsertPastes the clipboard text into line edit.
Ctrl+XDeletes the selected text and copies it to the clipboard.
Shift+DeleteDeletes the selected text and copies it to the clipboard.
Ctrl+ZUndoes the last operation.
Ctrl+YRedoes the last undone operation.

Any other key sequence that represents a valid character, will cause the character to be inserted into the line edit.

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

Member Type Documentation

enum QLineEdit::ActionPosition

This enum type describes how a line edit should display the action widgets to be added.

ConstantValueDescription
QLineEdit::LeadingPosition0The widget is displayed to the left of the text when using layout direction Qt::LeftToRight or to the right when using Qt::RightToLeft, respectively.
QLineEdit::TrailingPosition1The widget is displayed to the right of the text when using layout direction Qt::LeftToRight or to the left when using Qt::RightToLeft, respectively.

This enum was introduced or modified in Qt 5.2.

See also addAction(), removeAction(), and QWidget::layoutDirection.

enum QLineEdit::EchoMode

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

ConstantValueDescription
QLineEdit::Normal0Display characters as they are entered. This is the default.
QLineEdit::NoEcho1Do not display anything. This may be appropriate for passwords where even the length of the password should be kept secret.
QLineEdit::Password2Display platform-dependent password mask characters instead of the characters actually entered.
QLineEdit::PasswordEchoOnEdit3Display characters as they are entered while editing otherwise display characters as with Password.

See also setEchoMode() and echoMode().

Property Documentation

acceptableInput : const bool

This property holds whether the input satisfies the inputMask and the validator.

By default, this property is true.

Access functions:

bool hasAcceptableInput() const

See also setInputMask() and setValidator().

alignment : Qt::Alignment

This property holds the alignment of the line edit.

Both horizontal and vertical alignment is allowed here, Qt::AlignJustify will map to Qt::AlignLeft.

By default, this property contains a combination of Qt::AlignLeft and Qt::AlignVCenter.

Access functions:

Qt::Alignment alignment() const
void setAlignment(Qt::Alignment flag)

See also Qt::Alignment.

clearButtonEnabled : bool

This property holds whether the line edit displays a clear button when it is not empty.

If enabled, the line edit displays a trailing clear button when it contains some text, otherwise the line edit does not show a clear button (the default).

This property was introduced in Qt 5.2.

Access functions:

bool isClearButtonEnabled() const
void setClearButtonEnabled(bool enable)

See also addAction() and removeAction().

cursorMoveStyle : Qt::CursorMoveStyle

This property holds the movement style of cursor in this line edit.

When this property is set to Qt::VisualMoveStyle, the line edit will use visual movement style. Pressing the left arrow key will always cause the cursor to move left, regardless of the text's writing direction. The same behavior applies to right arrow key.

When the property is Qt::LogicalMoveStyle (the default), within a LTR text block, increase cursor position when pressing left arrow key, decrease cursor position when pressing the right arrow key. If the text block is right to left, the opposite behavior applies.

This property was introduced in Qt 4.8.

Access functions:

Qt::CursorMoveStyle cursorMoveStyle() const
void setCursorMoveStyle(Qt::CursorMoveStyle style)

cursorPosition : int

This property holds the current cursor position for this line edit.

Setting the cursor position causes a repaint when appropriate.

By default, this property contains a value of 0.

Access functions:

int cursorPosition() const
void setCursorPosition(int)

displayText : const QString

This property holds the displayed text.

If echoMode is Normal this returns the same as text(); if EchoMode is Password or PasswordEchoOnEdit it returns a string of platform-dependent password mask characters text().length() in size, e.g. "******"; if EchoMode is NoEcho returns an empty string, "".

By default, this property contains an empty string.

Access functions:

QString displayText() const

See also setEchoMode(), text(), and EchoMode.

dragEnabled : bool

This property holds whether the lineedit starts a drag if the user presses and moves the mouse on some selected text.

Dragging is disabled by default.

Access functions:

bool dragEnabled() const
void setDragEnabled(bool b)

echoMode : EchoMode

This property holds the line edit's echo mode.

The echo mode determines how the text entered in the line edit is displayed (or echoed) to the user.

The most common setting is Normal, in which the text entered by the user is displayed verbatim, but QLineEdit also supports modes that allow the entered text to be suppressed or obscured: these include NoEcho, Password and PasswordEchoOnEdit.

The widget's display and the ability to copy or drag the text is affected by this setting.

By default, this property is set to Normal.

Access functions:

QLineEdit::EchoMode echoMode() const
void setEchoMode(QLineEdit::EchoMode)

See also EchoMode and displayText().

frame : bool

This property holds whether the line edit draws itself with a frame.

If enabled (the default) the line edit draws itself inside a frame, otherwise the line edit draws itself without any frame.

Access functions:

bool hasFrame() const
void setFrame(bool)

hasSelectedText : const bool

This property holds whether there is any text selected.

hasSelectedText() returns true if some or all of the text has been selected by the user; otherwise returns false.

By default, this property is false.

Access functions:

bool hasSelectedText() const

See also selectedText().

inputMask : QString

This property holds the validation input mask.

If no mask is set, inputMask() returns an empty string.

Sets the QLineEdit's validation mask. Validators can be used instead of, or in conjunction with masks; see setValidator().

Unset the mask and return to normal QLineEdit operation by passing an empty string ("").

The input mask is an input template string. It can contain the following elements:

Mask CharactersDefines the Category of input characters that are considered valid in this position
Meta CharactersVarious special meanings
SeparatorsAll other characters are regarded as immutable separators

The following table shows the mask and meta characters that can be used in an input mask.

Mask CharacterMeaning
Acharacter of the Letter category required, such as A-Z, a-z.
acharacter of the Letter category permitted but not required.
Ncharacter of the Letter or Number category required, such as A-Z, a-z, 0-9.
ncharacter of the Letter or Number category permitted but not required.
XAny non-blank character required.
xAny non-blank character permitted but not required.
9character of the Number category required, e.g 0-9.
0character of the Number category permitted but not required.
Dcharacter of the Number category and larger than zero required, such as 1-9
dcharacter of the Number category and larger than zero permitted but not required, such as 1-9.
#character of the Number category, or plus/minus sign permitted but not required.
HHexadecimal character required. A-F, a-f, 0-9.
hHexadecimal character permitted but not required.
BBinary character required. 0-1.
bBinary character permitted but not required.
Meta CharacterMeaning
>All following alphabetic characters are uppercased.
<All following alphabetic characters are lowercased.
!Switch off case conversion.
;cTerminates the input mask and sets the blank character to c.
[ ] { }Reserved.
\Use \ to escape the special characters listed above to use them as separators.

When created or cleared, the line edit will be filled with a copy of the input mask string where the meta characters have been removed, and the mask characters have been replaced with the blank character (by default, a space).

When an input mask is set, the text() method returns a modified copy of the line edit content where all the blank characters have been removed. The unmodified content can be read using displayText().

The hasAcceptableInput() method returns false if the current content of the line edit does not fulfil the requirements of the input mask.

Examples:

MaskNotes
000.000.000.000;_IP address; blanks are _.
HH:HH:HH:HH:HH:HH;_MAC address
0000-00-00ISO Date; blanks are space
>AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;#License number; blanks are # and all (alphabetic) characters are converted to uppercase.

To get range control (e.g., for an IP address) use masks together with validators.

Access functions:

QString inputMask() const
void setInputMask(const QString &inputMask)

See also maxLength, QChar::isLetter(), QChar::isNumber(), and QChar::digitValue().

maxLength : int

This property holds the maximum permitted length of the text.

If the text is too long, it is truncated at the limit.

If truncation occurs any selected text will be unselected, the cursor position is set to 0 and the first part of the string is shown.

If the line edit has an input mask, the mask defines the maximum string length.

By default, this property contains a value of 32767.

Access functions:

int maxLength() const
void setMaxLength(int)

See also inputMask.

modified : bool

This property holds whether the line edit's contents has been modified by the user.

The modified flag is never read by QLineEdit; it has a default value of false and is changed to true whenever the user changes the line edit's contents.

This is useful for things that need to provide a default value but do not start out knowing what the default should be (perhaps it depends on other fields on the form). Start the line edit without the best default, and when the default is known, if modified() returns false (the user hasn't entered any text), insert the default value.

Calling setText() resets the modified flag to false.

Access functions:

bool isModified() const
void setModified(bool)

placeholderText : QString

This property holds the line edit's placeholder text.

Setting this property makes the line edit display a grayed-out placeholder text as long as the line edit is empty.

Normally, an empty line edit shows the placeholder text even when it has focus. However, if the content is horizontally centered, the placeholder text is not displayed under the cursor when the line edit has focus.

By default, this property contains an empty string.

This property was introduced in Qt 4.7.

Access functions:

QString placeholderText() const
void setPlaceholderText(const QString &)

See also text().

readOnly : bool

This property holds whether the line edit is read only.

In read-only mode, the user can still copy the text to the clipboard, or drag and drop the text (if echoMode() is Normal), but cannot edit it.

QLineEdit does not show a cursor in read-only mode.

By default, this property is false.

Access functions:

bool isReadOnly() const
void setReadOnly(bool)

See also setEnabled().

redoAvailable : const bool

This property holds whether redo is available.

Redo becomes available once the user has performed one or more undo operations on text in the line edit.

By default, this property is false.

Access functions:

bool isRedoAvailable() const

selectedText : const QString

This property holds the selected text.

If there is no selected text this property's value is an empty string.

By default, this property contains an empty string.

Access functions:

QString selectedText() const

See also hasSelectedText().

text : QString

This property holds the line edit's text.

Setting this property clears the selection, clears the undo/redo history, moves the cursor to the end of the line and resets the modified property to false. The text is not validated when inserted with setText().

The text is truncated to maxLength() length.

By default, this property contains an empty string.

Access functions:

QString text() const
void setText(const QString &)

Notifier signal:

void textChanged(const QString &)

See also insert() and clear().

undoAvailable : const bool

This property holds whether undo is available.

Undo becomes available once the user has modified the text in the line edit.

By default, this property is false.

Access functions:

bool isUndoAvailable() const