QLineEdit Class
The QLineEdit class is a one-line text editor. More…
| Header | #include <QLineEdit> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Widgets) |
| Module | Nt Widgets C++ Classes |
| Inherits | QWidget |
List of all members, including inherited members
Public Types
| enum | EchoMode |
Public Functions
| QLineEdit(QWidget *parent = nullptr) | |
| QLineEdit(const QString &text, QWidget *parent = nullptr) | |
| ~QLineEdit() | |
| QString | text() const |
| QString | displayText() const |
| QString | placeholderText() const |
| void | setCompleter(QCompleter *completer) |
| QCompleter * | completer() const |
| void | setPlaceholderText(const QString &text) |
| int | maxLength() const |
| void | setMaxLength(int length) |
| bool | isReadOnly() const |
| void | setReadOnly(bool readOnly) |
| bool | hasFrame() const |
| void | setFrame(bool frame) |
| EchoMode | echoMode() const |
| void | setEchoMode(EchoMode mode) |
| int | cursorPosition() const |
| void | setCursorPosition(int position) |
| int | cursorPositionAt(const QPoint &pos) const |
| bool | hasSelectedText() const |
| QString | selectedText() const |
| int | selectionStart() const |
| int | selectionEnd() const |
| void | setSelection(int start, int length) |
Reimplemented Public Functions
| QSize | sizeHint() const |
| QSize | minimumSizeHint() const |
Public Slots
| void | setText(const QString &text) |
| void | clear() |
| void | selectAll() |
| void | deselect() |
| void | insert(const QString &text) |
| void | backspace() |
| void | del() |
| void | home(bool mark = false) |
| void | end(bool mark = false) |
Signals
| void | textChanged(const QString &text) |
| void | textEdited(const QString &text) |
| void | returnPressed() |
| void | editingFinished() |
| void | cursorPositionChanged(int oldPos, int newPos) |
| void | selectionChanged() |
Reimplemented Protected Functions
| void | paintEvent(QPaintEvent *event) |
| void | mousePressEvent(QMouseEvent *event) |
| void | mouseMoveEvent(QMouseEvent *event) |
| void | mouseReleaseEvent(QMouseEvent *event) |
| void | mouseDoubleClickEvent(QMouseEvent *event) |
| void | keyPressEvent(QKeyEvent *event) |
| void | focusInEvent(QFocusEvent *event) |
| void | focusOutEvent(QFocusEvent *event) |
| void | changeEvent(QEvent *event) |
| void | resizeEvent(QResizeEvent *event) |
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.

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.
| Constant | Description |
|---|---|
QLineEdit::Normal | The characters are shown as they are. |
QLineEdit::NoEcho | Nothing is shown, which hides even the length of the text. |
QLineEdit::Password | A bullet is shown for each character. |
Member Function Documentation
QLineEdit::QLineEdit(QWidget *parent = nullptr)
Constructs an empty line edit with the given parent.
QLineEdit::QLineEdit(const QString &text, QWidget *parent = nullptr)
Constructs a line edit that contains text, with the given parent.
QLineEdit::~QLineEdit()
Destroys the line edit.
void QLineEdit::backspace()
Deletes the selection, or the character before the cursor if nothing is selected.
See also del().
void QLineEdit::changeEvent(QEvent *event)
Reimplements QWidget::changeEvent().
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().
void QLineEdit::cursorPositionChanged(int oldPos, int newPos)
This signal is emitted when the cursor moves from oldPos to newPos.
void QLineEdit::del()
Deletes the selection, or the character after the cursor if nothing is selected.
See also backspace().
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().
void QLineEdit::editingFinished()
This signal is emitted when Return or Enter is pressed, or the line edit loses the focus.
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().
void QLineEdit::focusInEvent(QFocusEvent *event)
Reimplements QWidget::focusInEvent().
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().
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().
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().
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().
QSize QLineEdit::minimumSizeHint() const
Reimplements QWidget::minimumSizeHint().
void QLineEdit::mouseDoubleClickEvent(QMouseEvent *event)
Reimplements QWidget::mouseDoubleClickEvent().
void QLineEdit::mouseMoveEvent(QMouseEvent *event)
Reimplements QWidget::mouseMoveEvent().
void QLineEdit::mousePressEvent(QMouseEvent *event)
Reimplements QWidget::mousePressEvent().
void QLineEdit::mouseReleaseEvent(QMouseEvent *event)
Reimplements QWidget::mouseReleaseEvent().
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().
void QLineEdit::resizeEvent(QResizeEvent *event)
Reimplements QWidget::resizeEvent().
void QLineEdit::returnPressed()
This signal is emitted when Return or Enter is pressed.
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().
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().
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().
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().
void QLineEdit::textChanged(const QString &text)
This signal is emitted when the text changes to text, whether the user edited it or setText() did.
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.