6.0.0-rc4Source

QPlainTextEdit Class

The QPlainTextEdit class is an editor for text of many lines. More…

Header#include <QPlainTextEdit>
CMaketarget_link_libraries(app PRIVATE Nt::Widgets)
ModuleNt Widgets C++ Classes
InheritsQAbstractScrollArea
Inherited byQTextEdit

List of all members, including inherited members

Public Types

Public Functions

QPlainTextEdit(QWidget *parent = nullptr)
QPlainTextEdit(const QString &text, QWidget *parent = nullptr)
~QPlainTextEdit()
QStringtoPlainText() const
QStringplaceholderText() const
voidsetPlaceholderText(const QString &text)
boolisReadOnly() const
voidsetReadOnly(bool readOnly)
LineWrapModelineWrapMode() const
voidsetLineWrapMode(LineWrapMode mode)
booltabChangesFocus() const
voidsetTabChangesFocus(bool enable)
qrealtabStopDistance() const
voidsetTabStopDistance(qreal distance)
boolisUndoRedoEnabled() const
voidsetUndoRedoEnabled(bool enable)
intblockCount() const
QTextCursortextCursor() const
voidsetTextCursor(const QTextCursor &cursor)
QTextCursorcursorForPosition(const QPoint &pos) const
QRectcursorRect() const
QRectcursorRect(const QTextCursor &cursor) const
voidmoveCursor(QTextCursor::MoveOperation operation, QTextCursor::MoveMode mode = QTextCursor::MoveAnchor)
voidensureCursorVisible()

Public Slots

voidsetPlainText(const QString &text)
voidappendPlainText(const QString &text)
voidinsertPlainText(const QString &text)
voidclear()
voidselectAll()
voidundo()
voidredo()

Signals

voidtextChanged()
voidcursorPositionChanged()
voidselectionChanged()
voidundoAvailable(bool available)
voidredoAvailable(bool available)
voidblockCountChanged(int newBlockCount)

Reimplemented Protected Functions

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.

QPlainTextEdit in the light color scheme

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.

ConstantDescription
QPlainTextEdit::NoWrapThe line goes on, and the editor has a horizontal scroll bar.
QPlainTextEdit::WidgetWidthThe line is broken at the width of the widget, at a space if there is one.

Member Function Documentation

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

Constructs an empty editor with the given parent.

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

Constructs an editor with the given text and parent.

[override] QPlainTextEdit::~QPlainTextEdit()

Destroys the editor.

[slot] 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.

[signal] void QPlainTextEdit::blockCountChanged(int newBlockCount)

This signal is emitted when the number of blocks (lines separated by a new line) changes to newBlockCount.

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

Reimplements QAbstractScrollArea::changeEvent().

[slot] 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.

[signal] 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().

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

Reimplements QAbstractScrollArea::focusInEvent().

[override, protected] bool QPlainTextEdit::focusNextPrevChild(bool next)

Reimplements QAbstractScrollArea::focusNextPrevChild().

A tab is typed in the editor, unless tabChangesFocus() is set.

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

Reimplements QAbstractScrollArea::focusOutEvent().

[slot] 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().

[override, protected] 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().

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

Reimplements QAbstractScrollArea::mouseDoubleClickEvent().

A double click selects the word under the mouse.

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

Reimplements QAbstractScrollArea::mouseMoveEvent().

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

Reimplements QAbstractScrollArea::mousePressEvent().

[override, protected] 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().

[override, protected] 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().

[slot] void QPlainTextEdit::redo()

Does again the change that undo() undid. Does nothing if there is none.

See also undo().

[signal] void QPlainTextEdit::redoAvailable(bool available)

This signal is emitted when redo() becomes possible, with available true, or impossible.

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

Reimplements QAbstractScrollArea::resizeEvent().

[override, protected] void QPlainTextEdit::scrollContentsBy(int dx, int dy)

Reimplements QAbstractScrollArea::scrollContentsBy().

[slot] void QPlainTextEdit::selectAll()

Selects all the text, with the cursor at the end.

[signal] 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().

[slot] 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().

[override, protected] 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().

[signal] 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().

[slot] 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().

[signal] void QPlainTextEdit::undoAvailable(bool available)

This signal is emitted when undo() becomes possible, with available true, or impossible.