6.0.0-rc4Source

QLineEdit Class

The QLineEdit class is a one-line text editor. More…

Header#include <QLineEdit>
CMaketarget_link_libraries(app PRIVATE Nt::Widgets)
ModuleNt Widgets C++ Classes
InheritsQWidget

List of all members, including inherited members

Public Types

enumEchoMode

Public Functions

QLineEdit(QWidget *parent = nullptr)
QLineEdit(const QString &text, QWidget *parent = nullptr)
~QLineEdit()
QStringtext() const
QStringdisplayText() const
QStringplaceholderText() const
voidsetCompleter(QCompleter *completer)
QCompleter *completer() const
voidsetPlaceholderText(const QString &text)
intmaxLength() const
voidsetMaxLength(int length)
boolisReadOnly() const
voidsetReadOnly(bool readOnly)
boolhasFrame() const
voidsetFrame(bool frame)
EchoModeechoMode() const
voidsetEchoMode(EchoMode mode)
intcursorPosition() const
voidsetCursorPosition(int position)
intcursorPositionAt(const QPoint &pos) const
boolhasSelectedText() const
QStringselectedText() const
intselectionStart() const
intselectionEnd() const
voidsetSelection(int start, int length)

Reimplemented Public Functions

Public Slots

voidsetText(const QString &text)
voidclear()
voidselectAll()
voiddeselect()
voidinsert(const QString &text)
voidbackspace()
voiddel()
voidhome(bool mark = false)
voidend(bool mark = false)

Signals

voidtextChanged(const QString &text)
voidtextEdited(const QString &text)
voidreturnPressed()
voideditingFinished()
voidcursorPositionChanged(int oldPos, int newPos)
voidselectionChanged()

Reimplemented Protected Functions

Detailed Description

A line edit shows a text that the user can edit: type to insert, use the arrow keys, Home, End, Backspace and Delete to move and remove text, hold Shift to select, and select with the mouse. Connect to textChanged() for every change, to textEdited() for the changes the user makes, and to returnPressed() or editingFinished() for the end of an edit:

QLineEdit *name = new QLineEdit(window);
name->setPlaceholderText("Your name");
connect(name, &QLineEdit::textChanged, [](const QString &text) { qDebug() << text; });

Line edits: with a placeholder, with the cursor, with a selection, in password mode, read-only, and disabled.

QLineEdit in the light color scheme

See also QLabel.

Differences from Qt

There is no clipboard (cut, copy, paste), undo and redo, drag and drop, context menu, input mask, QValidator, QCompleter, alignment, setClearButtonEnabled() or inputMethodHints(). The cursor does not blink, and there are no PasswordEchoOnEdit and no cursorMoveStyle(). Text is edited one code unit at a time, so characters outside the Basic Multilingual Plane count twice.

Member Type Documentation

enum QLineEdit::EchoMode

This enum says how the text of a line edit is shown.

ConstantDescription
QLineEdit::NormalThe characters are shown as they are.
QLineEdit::NoEchoNothing is shown, which hides even the length of the text.
QLineEdit::PasswordA bullet is shown for each character.

Member Function Documentation

[explicit] QLineEdit::QLineEdit(QWidget *parent = nullptr)

Constructs an empty line edit with the given parent.

[explicit] QLineEdit::QLineEdit(const QString &text, QWidget *parent = nullptr)

Constructs a line edit that contains text, with the given parent.

[override] QLineEdit::~QLineEdit()

Destroys the line edit.

[slot] void QLineEdit::backspace()

Deletes the selection, or the character before the cursor if nothing is selected.

See also del().

[override, protected] void QLineEdit::changeEvent(QEvent *event)

Reimplements QWidget::changeEvent().

[slot] void QLineEdit::clear()

Removes the text.

QCompleter * QLineEdit::completer() const

Returns the completer of the line edit, or nullptr.

See also setCompleter().

int QLineEdit::cursorPosition() const

Returns the position of the cursor in the text, from 0 for before the first character to the length of the text.

See also setCursorPosition().

int QLineEdit::cursorPositionAt(const QPoint &pos) const

Returns the cursor position that pos, a point in the line edit, is closest to.

See also setCursorPosition().

[signal] void QLineEdit::cursorPositionChanged(int oldPos, int newPos)

This signal is emitted when the cursor moves from oldPos to newPos.

[slot] void QLineEdit::del()

Deletes the selection, or the character after the cursor if nothing is selected.

See also backspace().

[slot] void QLineEdit::deselect()

Clears the selection and keeps the cursor where it is.

See also selectAll().

QString QLineEdit::displayText() const

Returns the text as it is shown: the same as text(), a bullet for each character with the Password echo mode, or nothing with NoEcho.

See also echoMode().

EchoMode QLineEdit::echoMode() const

Returns how the text is shown.

See also setEchoMode().

[signal] void QLineEdit::editingFinished()

This signal is emitted when Return or Enter is pressed, or the line edit loses the focus.

[slot] void QLineEdit::end(bool mark = false)

Moves the cursor to the end of the text, and selects what it moves over if mark is true.

See also home().

[override, protected] void QLineEdit::focusInEvent(QFocusEvent *event)

Reimplements QWidget::focusInEvent().

[override, protected] void QLineEdit::focusOutEvent(QFocusEvent *event)

Reimplements QWidget::focusOutEvent().

bool QLineEdit::hasFrame() const

Returns whether the line edit draws a frame around its text. The default is true.

See also setFrame().

bool QLineEdit::hasSelectedText() const

Returns whether some of the text is selected.

See also selectedText().

[slot] void QLineEdit::home(bool mark = false)

Moves the cursor to the beginning of the text, and selects what it moves over if mark is true.

See also end().

[slot] void QLineEdit::insert(const QString &text)

Inserts text at the cursor, in place of the selection, as far as maxLength() allows.

See also backspace() and del().

bool QLineEdit::isReadOnly() const

Returns whether the user cannot change the text. The text can still be selected.

See also setReadOnly().

[override, protected] void QLineEdit::keyPressEvent(QKeyEvent *event)

Reimplements QWidget::keyPressEvent().

int QLineEdit::maxLength() const

Returns the longest text the line edit accepts, 32767 characters by default.

See also setMaxLength().

[override] QSize QLineEdit::minimumSizeHint() const

Reimplements QWidget::minimumSizeHint().

[override, protected] void QLineEdit::mouseDoubleClickEvent(QMouseEvent *event)

Reimplements QWidget::mouseDoubleClickEvent().

[override, protected] void QLineEdit::mouseMoveEvent(QMouseEvent *event)

Reimplements QWidget::mouseMoveEvent().

[override, protected] void QLineEdit::mousePressEvent(QMouseEvent *event)

Reimplements QWidget::mousePressEvent().

[override, protected] void QLineEdit::mouseReleaseEvent(QMouseEvent *event)

Reimplements QWidget::mouseReleaseEvent().

[override, protected] void QLineEdit::paintEvent(QPaintEvent *event)

Reimplements QWidget::paintEvent().

QString QLineEdit::placeholderText() const

Returns the text that is shown in gray while the line edit is empty.

See also setPlaceholderText().

[override, protected] void QLineEdit::resizeEvent(QResizeEvent *event)

Reimplements QWidget::resizeEvent().

[signal] void QLineEdit::returnPressed()

This signal is emitted when Return or Enter is pressed.

[slot] void QLineEdit::selectAll()

Selects all of the text and puts the cursor at its end.

See also deselect().

QString QLineEdit::selectedText() const

Returns the selected text, or an empty string if nothing is selected.

See also hasSelectedText().

[signal] void QLineEdit::selectionChanged()

This signal is emitted when the selection changes.

int QLineEdit::selectionEnd() const

Returns where the selection ends, or the position of the cursor if nothing is selected.

See also selectionStart().

int QLineEdit::selectionStart() const

Returns where the selection starts, or the position of the cursor if nothing is selected.

See also selectionEnd().

void QLineEdit::setCompleter(QCompleter *completer)

Makes completer complete what the user types, or removes the completer with nullptr. The line edit does not own the completer.

See also completer() and QCompleter.

void QLineEdit::setCursorPosition(int position)

Moves the cursor to position, which is limited to the text, and clears the selection.

See also cursorPosition().

void QLineEdit::setEchoMode(EchoMode mode)

Sets how the text is shown to mode.

See also echoMode().

void QLineEdit::setFrame(bool frame)

Sets whether the line edit has a frame to frame.

See also hasFrame().

void QLineEdit::setMaxLength(int length)

Sets the longest text the line edit accepts to length characters. A text that is longer is cut.

See also maxLength().

void QLineEdit::setPlaceholderText(const QString &text)

Sets the text that is shown while the line edit is empty to text.

See also placeholderText().

void QLineEdit::setReadOnly(bool readOnly)

Sets whether the text is read-only to readOnly.

See also isReadOnly().

void QLineEdit::setSelection(int start, int length)

Selects length characters from start. A negative length selects backwards from start, with the cursor at the beginning of the selection.

See also selectAll() and deselect().

[slot] void QLineEdit::setText(const QString &text)

Sets the text to text, cuts it to maxLength() and puts the cursor at its end. The text counts as not edited by the user, so textEdited() is not emitted.

See also text().

[override] QSize QLineEdit::sizeHint() const

Reimplements QWidget::sizeHint().

QString QLineEdit::text() const

Returns the text. The default is an empty string.

See also setText() and displayText().

[signal] void QLineEdit::textChanged(const QString &text)

This signal is emitted when the text changes to text, whether the user edited it or setText() did.

[signal] void QLineEdit::textEdited(const QString &text)

This signal is emitted when the user edited the text, and it changed to text. It is not emitted for changes that setText() makes.