6.0.0-rc4Source

QTextEdit Class

The QTextEdit class shows rich text, and edits plain text. More…

Header#include <QTextEdit>
CMaketarget_link_libraries(app PRIVATE Nt::Widgets)
ModuleNt Widgets C++ Classes
InheritsQPlainTextEdit
Inherited byQTextBrowser

List of all members, including inherited members

Public Functions

QTextEdit(QWidget *parent = nullptr)
QTextEdit(const QString &text, QWidget *parent = nullptr)
~QTextEdit()
boolacceptRichText() const
voidsetAcceptRichText(bool accept)
QStringtoHtml() const
QTextDocument *document() const
QStringdocumentTitle() const
voidsetDocumentTitle(const QString &title)
QStringanchorAt(const QPoint &pos) const

Public Slots

voidsetText(const QString &text)
voidsetHtml(const QString &text)
voidappend(const QString &text)

Reimplemented Protected Functions

Detailed Description

A QTextEdit is a QPlainTextEdit that also shows rich text. setHtml() gives it HTML, which it shows with its fonts, colors, headings, lists and links, through a QTextDocument; QTextDocument says what of HTML is understood. setText() takes either kind of text, and append() adds a line:

QTextEdit *view = new QTextEdit(window);
view->setReadOnly(true);
view->setHtml("<h3>Result</h3><p>All <b>12</b> tests passed.</p>");

toHtml() returns the text as HTML, and document() the document.

See also QPlainTextEdit, QTextBrowser and QTextDocument.

Differences from Qt

Rich text is shown, not edited: there is no QTextCursor over the document, and text with formats has no selection. When the text is changed in another way than by setHtml(), by typing for one, the editor goes on with the text alone, as a QPlainTextEdit: the formats are gone. setMarkdown(), insertHtml(), setCurrentFont(), setTextColor() and the other functions that set the format of text are not there, and acceptRichText() does nothing. The document that document() returns is that of the editor only for reading and for giving it other contents as a whole. A QTextEdit is a QPlainTextEdit here; in Qt the two classes are not related.

Member Function Documentation

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

Constructs an empty editor with the given parent.

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

Constructs an editor with the given text and parent. The text is plain text or HTML, as for setText().

[override] QTextEdit::~QTextEdit()

Destroys the editor.

bool QTextEdit::acceptRichText() const

Returns whether the editor takes rich text from the clipboard. It has no effect.

See also setAcceptRichText().

QString QTextEdit::anchorAt(const QPoint &pos) const

Returns the target of the link at pos, in the coordinates of the viewport, or an empty string if there is no link there.

[slot] void QTextEdit::append(const QString &text)

Appends a new line with text, as appendPlainText() does. The formats of rich text that was shown are gone then.

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

Reimplements QPlainTextEdit::changeEvent().

QTextDocument * QTextEdit::document() const

Returns the document of the editor, which has the text and its formats. The editor owns it. Giving the document other contents, with QTextDocument::setHtml() for example, shows them.

QString QTextEdit::documentTitle() const

Returns the title of the document, which setHtml() reads from the title element.

See also setDocumentTitle().

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

Reimplements QPlainTextEdit::mouseDoubleClickEvent().

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

Reimplements QPlainTextEdit::mouseMoveEvent().

Over a link of rich text the cursor is the pointing hand.

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

Reimplements QPlainTextEdit::mousePressEvent().

Rich text has no cursor to put somewhere: the press is taken, and does nothing.

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

Reimplements QPlainTextEdit::mouseReleaseEvent().

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

Reimplements QPlainTextEdit::paintEvent().

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

Reimplements QPlainTextEdit::resizeEvent().

void QTextEdit::setAcceptRichText(bool accept)

Sets accept, which has no effect.

See also acceptRichText().

void QTextEdit::setDocumentTitle(const QString &title)

Sets the title of the document to title.

See also documentTitle().

[slot] void QTextEdit::setHtml(const QString &text)

Shows the HTML in text, with its formats. toPlainText() is its text without them.

See also toHtml() and QTextDocument::setHtml().

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

Sets the text to text: as rich text if Qt::mightBeRichText() takes it for HTML, as setHtml() does, and as plain text otherwise, as setPlainText() does.

QString QTextEdit::toHtml() const

Returns the text as HTML: with its formats if it was set as rich text, and as paragraphs of plain text otherwise.

See also setHtml() and toPlainText().