QPlainTextEdit Class
The QPlainTextEdit class is an editor for text of many lines. More…
| Header | #include <QPlainTextEdit> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Widgets) |
| Module | Nt Widgets C++ Classes |
| Inherits | QAbstractScrollArea |
| Inherited by | QTextEdit |
List of all members, including inherited members
Public Types
| enum | LineWrapMode |
Public Functions
| QPlainTextEdit(QWidget *parent = nullptr) | |
| QPlainTextEdit(const QString &text, QWidget *parent = nullptr) | |
| ~QPlainTextEdit() | |
| QString | toPlainText() const |
| QString | placeholderText() const |
| void | setPlaceholderText(const QString &text) |
| bool | isReadOnly() const |
| void | setReadOnly(bool readOnly) |
| LineWrapMode | lineWrapMode() const |
| void | setLineWrapMode(LineWrapMode mode) |
| bool | tabChangesFocus() const |
| void | setTabChangesFocus(bool enable) |
| qreal | tabStopDistance() const |
| void | setTabStopDistance(qreal distance) |
| bool | isUndoRedoEnabled() const |
| void | setUndoRedoEnabled(bool enable) |
| int | blockCount() const |
| QTextCursor | textCursor() const |
| void | setTextCursor(const QTextCursor &cursor) |
| QTextCursor | cursorForPosition(const QPoint &pos) const |
| QRect | cursorRect() const |
| QRect | cursorRect(const QTextCursor &cursor) const |
| void | moveCursor(QTextCursor::MoveOperation operation, QTextCursor::MoveMode mode = QTextCursor::MoveAnchor) |
| void | ensureCursorVisible() |
Public Slots
| void | setPlainText(const QString &text) |
| void | appendPlainText(const QString &text) |
| void | insertPlainText(const QString &text) |
| void | clear() |
| void | selectAll() |
| void | undo() |
| void | redo() |
Signals
| void | textChanged() |
| void | cursorPositionChanged() |
| void | selectionChanged() |
| void | undoAvailable(bool available) |
| void | redoAvailable(bool available) |
| void | blockCountChanged(int newBlockCount) |
Reimplemented Protected Functions
| void | paintEvent(QPaintEvent *event) |
| void | resizeEvent(QResizeEvent *event) |
| void | showEvent(QShowEvent *event) |
| void | scrollContentsBy(int dx, int dy) |
| 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) |
| bool | focusNextPrevChild(bool next) |
Detailed Description
The user types, moves the cursor with the keys or the mouse, selects, and undoes. Long lines are wrapped at the width of the widget unless the wrap mode is changed. Read and set the text with toPlainText() and setPlainText(), and connect to textChanged():
QPlainTextEdit *edit = new QPlainTextEdit(window);
edit->setPlaceholderText("Write something");
edit->appendPlainText("A first line.");
connect(edit, &QPlainTextEdit::textChanged, [edit] { qDebug() << edit->toPlainText().size(); });
The cursor is a QTextCursor, which can move through the text and change it. A log is a read-only editor to which lines are appended with appendPlainText(); it scrolls to follow the new text if it was at the bottom.
A text edit with a selection, one with a placeholder and one that is read-only.

See also QTextEdit, QTextCursor and QLineEdit.
Differences from Qt
The text has no formatting, no fonts or colors of parts of it, and there is no QTextDocument: the text is one string, and positions are the number of characters from its start, a new line counting as one. The clipboard is not there yet, so there are no cut(), copy() or paste(), and no drag and drop. There is no find(), no maximumBlockCount, no setCursorWidth(), and no context menu. The cursor does not blink. A selection that has several lines returns them separated by a new line from QTextCursor::selectedText(), where Qt uses the paragraph separator.
Member Type Documentation
enum QPlainTextEdit::LineWrapMode
This enum says what happens to a line that is longer than the widget.
| Constant | Description |
|---|---|
QPlainTextEdit::NoWrap | The line goes on, and the editor has a horizontal scroll bar. |
QPlainTextEdit::WidgetWidth | The line is broken at the width of the widget, at a space if there is one. |
Member Function Documentation
QPlainTextEdit::QPlainTextEdit(QWidget *parent = nullptr)
Constructs an empty editor with the given parent.
QPlainTextEdit::QPlainTextEdit(const QString &text, QWidget *parent = nullptr)
Constructs an editor with the given text and parent.
QPlainTextEdit::~QPlainTextEdit()
Destroys the editor.
void QPlainTextEdit::appendPlainText(const QString &text)
Appends a new line with text to the end of the text. If the view was scrolled to the bottom, it stays there.
See also insertPlainText().
int QPlainTextEdit::blockCount() const
Returns the number of blocks: lines that are separated by a new line character. There is at least one.
void QPlainTextEdit::blockCountChanged(int newBlockCount)
This signal is emitted when the number of blocks (lines separated by a new line) changes to newBlockCount.
void QPlainTextEdit::changeEvent(QEvent *event)
Reimplements QAbstractScrollArea::changeEvent().
void QPlainTextEdit::clear()
Removes all the text and the undo history.
See also setPlainText().
QTextCursor QPlainTextEdit::cursorForPosition(const QPoint &pos) const
Returns a cursor at the text position that is under pos, a point in the viewport.
void QPlainTextEdit::cursorPositionChanged()
This signal is emitted when the position of the cursor changes.
QRect QPlainTextEdit::cursorRect() const
Returns the rectangle of the cursor in the viewport: one pixel wide, and as high as a line.
See also ensureCursorVisible().
QRect QPlainTextEdit::cursorRect(const QTextCursor &cursor) const
This is an overloaded function.
Returns the rectangle of cursor in the viewport.
void QPlainTextEdit::ensureCursorVisible()
Scrolls the editor so that the cursor is in view.
See also cursorRect().
void QPlainTextEdit::focusInEvent(QFocusEvent *event)
Reimplements QAbstractScrollArea::focusInEvent().
bool QPlainTextEdit::focusNextPrevChild(bool next)
Reimplements QAbstractScrollArea::focusNextPrevChild().
A tab is typed in the editor, unless tabChangesFocus() is set.
void QPlainTextEdit::focusOutEvent(QFocusEvent *event)
Reimplements QAbstractScrollArea::focusOutEvent().
void QPlainTextEdit::insertPlainText(const QString &text)
Inserts text at the cursor, in place of the selection if there is one, and puts the cursor after it.
See also appendPlainText().
bool QPlainTextEdit::isReadOnly() const
Returns whether the user cannot change the text. The program still can. The default is false.
See also setReadOnly().
bool QPlainTextEdit::isUndoRedoEnabled() const
Returns whether undo() and redo() work. The default is true.
See also setUndoRedoEnabled().
void QPlainTextEdit::keyPressEvent(QKeyEvent *event)
Reimplements QAbstractScrollArea::keyPressEvent().
The arrow keys move the cursor, Control with them by a word, and Shift selects; Home and End go to the start and the end of the line, and with Control of the text; Page Up and Page Down move by a page. Control+A selects all, Control+Z undoes, and Control+Y or Control+Shift+Z does again. Unless the editor is read-only, Backspace and Delete remove a character (Control, a word), Return starts a line, Tab types a tab, and other keys type their text.
LineWrapMode QPlainTextEdit::lineWrapMode() const
Returns how long lines are shown. The default is WidgetWidth.
See also setLineWrapMode().
void QPlainTextEdit::mouseDoubleClickEvent(QMouseEvent *event)
Reimplements QAbstractScrollArea::mouseDoubleClickEvent().
A double click selects the word under the mouse.
void QPlainTextEdit::mouseMoveEvent(QMouseEvent *event)
Reimplements QAbstractScrollArea::mouseMoveEvent().
void QPlainTextEdit::mousePressEvent(QMouseEvent *event)
Reimplements QAbstractScrollArea::mousePressEvent().
void QPlainTextEdit::mouseReleaseEvent(QMouseEvent *event)
Reimplements QAbstractScrollArea::mouseReleaseEvent().
void QPlainTextEdit::moveCursor(QTextCursor::MoveOperation operation, QTextCursor::MoveMode mode = QTextCursor::MoveAnchor)
Moves the cursor by operation, extending the selection if mode is KeepAnchor.
See also textCursor().
void QPlainTextEdit::paintEvent(QPaintEvent *event)
Reimplements QAbstractScrollArea::paintEvent().
QString QPlainTextEdit::placeholderText() const
Returns the text that is shown, dimmed, while the editor is empty.
See also setPlaceholderText().
void QPlainTextEdit::redo()
Does again the change that undo() undid. Does nothing if there is none.
See also undo().
void QPlainTextEdit::redoAvailable(bool available)
This signal is emitted when redo() becomes possible, with available true, or impossible.
void QPlainTextEdit::resizeEvent(QResizeEvent *event)
Reimplements QAbstractScrollArea::resizeEvent().
void QPlainTextEdit::scrollContentsBy(int dx, int dy)
Reimplements QAbstractScrollArea::scrollContentsBy().
void QPlainTextEdit::selectAll()
Selects all the text, with the cursor at the end.
void QPlainTextEdit::selectionChanged()
This signal is emitted when the selection changes.
void QPlainTextEdit::setLineWrapMode(LineWrapMode mode)
Sets how long lines are shown to mode.
See also lineWrapMode().
void QPlainTextEdit::setPlaceholderText(const QString &text)
Sets the text that is shown while the editor is empty to text.
See also placeholderText().
void QPlainTextEdit::setPlainText(const QString &text)
Replaces the text with text, and puts the cursor at the start. The undo history is cleared. Emits textChanged() if the text is not the same.
See also toPlainText() and clear().
void QPlainTextEdit::setReadOnly(bool readOnly)
Sets whether the user cannot change the text to readOnly. The cursor can still be moved, and text selected.
See also isReadOnly().
void QPlainTextEdit::setTabChangesFocus(bool enable)
Sets whether the Tab key moves the focus to enable.
See also tabChangesFocus().
void QPlainTextEdit::setTabStopDistance(qreal distance)
Sets the distance between tab stops to distance pixels.
See also tabStopDistance().
void QPlainTextEdit::setTextCursor(const QTextCursor &cursor)
Makes the position and the selection of cursor those of the editor.
See also textCursor().
void QPlainTextEdit::setUndoRedoEnabled(bool enable)
Sets whether the changes are kept so that they can be undone to enable. Turning it off clears the history.
See also isUndoRedoEnabled().
void QPlainTextEdit::showEvent(QShowEvent *event)
Reimplements QAbstractScrollArea::showEvent().
bool QPlainTextEdit::tabChangesFocus() const
Returns whether the Tab key moves the focus to the next widget, instead of typing a tab. The default is false.
See also setTabChangesFocus().
qreal QPlainTextEdit::tabStopDistance() const
Returns the distance between tab stops in pixels, 80 by default.
See also setTabStopDistance().
void QPlainTextEdit::textChanged()
This signal is emitted when the text changes.
QTextCursor QPlainTextEdit::textCursor() const
Returns a cursor at the position of the editor's own cursor, and with its selection. A change to the cursor that is returned moves the editor's cursor only after setTextCursor().
See also setTextCursor().
QString QPlainTextEdit::toPlainText() const
Returns the text. Lines are separated by a new line character.
See also setPlainText().
void QPlainTextEdit::undo()
Undoes the last change: the text that was typed since the cursor moved, or a change of the program. Does nothing if there is nothing to undo.
See also redo().
void QPlainTextEdit::undoAvailable(bool available)
This signal is emitted when undo() becomes possible, with available true, or impossible.