KRichTextEdit Class

A KTextEdit that supports both rich text and plain text modes. More...

Header: #include <KRichTextEdit>
CMake: find_package(KF6 REQUIRED COMPONENTS TextWidgets)
target_link_libraries(mytarget PRIVATE KF6::TextWidgets)
Since: 4.1
Inherits: KTextEdit
Inherited By:

KRichTextWidget

Public Types

enum Mode { Plain, Rich }

Public Functions

KRichTextEdit(QWidget *parent = nullptr)
KRichTextEdit(const QString &text, QWidget *parent = nullptr)
bool canDedentList() const
bool canIndentList() const
QString currentLinkText() const
QString currentLinkUrl() const
void enableRichTextMode()
void selectLinkText() const
void selectLinkText(QTextCursor *cursor) const
void setTextOrHtml(const QString &text)
KRichTextEdit::Mode textMode() const
QString textOrHtml() const
void updateLink(const QString &linkUrl, const QString &linkText)

Public Slots

void alignCenter()
void alignJustify()
void alignLeft()
void alignRight()
void indentListLess()
void indentListMore()
void insertHorizontalRule()
(since 4.10) void insertPlainTextImplementation()
(since 4.6) void makeLeftToRight()
(since 4.6) void makeRightToLeft()
void setFont(const QFont &font)
void setFontFamily(const QString &fontFamily)
void setFontSize(int size)
(since 5.70) void setHeadingLevel(int level)
void setListStyle(int _styleIndex)
void setTextBackgroundColor(const QColor &color)
void setTextBold(bool bold)
void setTextForegroundColor(const QColor &color)
void setTextItalic(bool italic)
void setTextStrikeOut(bool strikeOut)
void setTextSubScript(bool subscript)
void setTextSuperScript(bool superscript)
void setTextUnderline(bool underline)
void switchToPlainText()
QString toCleanHtml() const

Signals

void textModeChanged(KRichTextEdit::Mode mode)

Detailed Description

The KRichTextEdit class provides a widget to edit and display rich text.

It offers several additional rich text editing functions to KTextEdit and makes them easier to access including:

  • Changing fonts, sizes.
  • Font formatting, such as bold, underline, italic, foreground and background color.
  • Paragraph alignment
  • Ability to edit and remove hyperlinks
  • Nested list handling
  • Simple actions to insert tables. TODO

The KRichTextEdit can be in two modes: Rich text mode and plain text mode. Calling functions which modify the format/style of the text will automatically enable the rich text mode. Rich text mode is sometimes also referred to as HTML mode.

Do not call setAcceptRichText() or acceptRichText() yourself. Instead simply connect to the slots which insert the rich text, use switchToPlainText() or enableRichTextMode().

"KRichTextEdit Widget"

Member Type Documentation

enum KRichTextEdit::Mode

The mode the edit widget is in.

ConstantValueDescription
KRichTextEdit::Plain0Plain text mode
KRichTextEdit::Rich1Rich text mode

Member Function Documentation

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

Constructs a KRichTextEdit object.

parent The parent widget

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

Constructs a KRichTextEdit object

text The initial text of the text edit, which is interpreted as HTML.

parent The parent widget

[slot] void KRichTextEdit::alignCenter()

Sets the alignment of the current block to Centered

[slot] void KRichTextEdit::alignJustify()

Sets the alignment of the current block to Justified

[slot] void KRichTextEdit::alignLeft()

Sets the alignment of the current block to Left Aligned

[slot] void KRichTextEdit::alignRight()

Sets the alignment of the current block to Right Aligned

bool KRichTextEdit::canDedentList() const

Returns true if the list item at the current position can be dedented.

See also canIndentList.

bool KRichTextEdit::canIndentList() const

Returns true if the list item at the current position can be indented.

See also canDedentList.

QString KRichTextEdit::currentLinkText() const

Returns the text of the link at the current position or an empty string if the cursor is not on a link.

See also currentLinkUrl.

QString KRichTextEdit::currentLinkUrl() const

Returns the URL target (href) of the link at the current position or an empty string if the cursor is not on a link.

See also currentLinkText.

void KRichTextEdit::enableRichTextMode()

This enables rich text mode. Nothing is done except changing the internal mode and allowing rich text pastes.

[slot] void KRichTextEdit::indentListLess()

Decreases the nesting level of the current block or selected blocks.

See also canDedentList.

[slot] void KRichTextEdit::indentListMore()

Increases the nesting level of the current block or selected blocks.

See also canIndentList.

[slot] void KRichTextEdit::insertHorizontalRule()

Inserts a horizontal rule below the current block.

[slot, since 4.10] void KRichTextEdit::insertPlainTextImplementation()

Because of binary compatibility constraints, insertPlainText is not virtual. Therefore it must dynamically detect and call this slot.

This function was introduced in 4.10.

[slot, since 4.6] void KRichTextEdit::makeLeftToRight()

Sets the direction of the current block to Left-To-Right

This function was introduced in 4.6.

[slot, since 4.6] void KRichTextEdit::makeRightToLeft()

Sets the direction of the current block to Right-To-Left

This function was introduced in 4.6.

void KRichTextEdit::selectLinkText() const

Convenience function to select the link text using the active cursor.

See also selectLinkText.

void KRichTextEdit::selectLinkText(QTextCursor *cursor) const

If the cursor is on a link, sets the cursor to a selection of the text of the link. If the cursor is not on a link, selects the current word or existing selection.

cursor The cursor to use to select the text.

See also updateLink.

[slot] void KRichTextEdit::setFont(const QFont &font)

Sets the current word or selection to the font font

font the font of the text will be set to this font

[slot] void KRichTextEdit::setFontFamily(const QString &fontFamily)

Sets the current word or selection to the font family fontFamily

fontFamily The text's font family will be changed to this one

[slot] void KRichTextEdit::setFontSize(int size)

Sets the current word or selection to the font size size

size The text's font will get this size

[slot, since 5.70] void KRichTextEdit::setHeadingLevel(int level)

Sets the heading level of a current block or selection

level Heading level (value should be between 0 and 6) (0 is "normal text", 1 is the largest heading, 6 is the smallest one)

This function was introduced in 5.70.

[slot] void KRichTextEdit::setListStyle(int _styleIndex)

Sets the list style of the current list, or creates a new list using the current block. The _styleindex corresponds to the QTextListFormat::Style

_styleIndex The list will get this style

[slot] void KRichTextEdit::setTextBackgroundColor(const QColor &color)

Sets the background color of the current word or selection to color.

color The text will get this foreground color

[slot] void KRichTextEdit::setTextBold(bool bold)

Toggles the bold formatting of the current word or selection at the current cursor position.

bold If true, the text will be set to bold

[slot] void KRichTextEdit::setTextForegroundColor(const QColor &color)

Sets the foreground color of the current word or selection to color.

color The text will get this background color

[slot] void KRichTextEdit::setTextItalic(bool italic)

Toggles the italic formatting of the current word or selection at the current cursor position.

italic If true, the text will be set to italic

void KRichTextEdit::setTextOrHtml(const QString &text)

Replaces all the content of the text edit with the given string. If the string is in rich text format, the text is inserted as rich text, otherwise it is inserted as plain text.

text The text to insert

See also textOrHtml().

[slot] void KRichTextEdit::setTextStrikeOut(bool strikeOut)

Toggles the strikeout formatting of the current word or selection at the current cursor position.

strikeOut If true, the text will be struck out

[slot] void KRichTextEdit::setTextSubScript(bool subscript)

Toggles the subscript formatting of the current word or selection at the current cursor position.

subscript If true, the text will be set to subscript

[slot] void KRichTextEdit::setTextSuperScript(bool superscript)

Toggles the superscript formatting of the current word or selection at the current cursor position.

superscript If true, the text will be set to superscript

[slot] void KRichTextEdit::setTextUnderline(bool underline)

Toggles the underline formatting of the current word or selection at the current cursor position.

underline If true, the text will be underlined

[slot] void KRichTextEdit::switchToPlainText()

This will switch the editor to plain text mode. All rich text formatting will be destroyed.

KRichTextEdit::Mode KRichTextEdit::textMode() const

Returns the current text mode

[signal] void KRichTextEdit::textModeChanged(KRichTextEdit::Mode mode)

Emitted whenever the text mode is changed.

mode The new text mode

QString KRichTextEdit::textOrHtml() const

Returns the plain text string if in plain text mode or the HTML code if in rich text mode. The text is not word-wrapped.

See also setTextOrHtml().

[slot] QString KRichTextEdit::toCleanHtml() const

This will clean some of the bad html produced by the underlying QTextEdit It walks over all lines and cleans up a bit. Should be improved to produce our own Html.

Replaces the current selection with a hyperlink with the link URL linkUrl and the link text linkText.

linkUrl The link will get this URL as href (target)

linkText The link will get this alternative text, which is the text displayed in the text edit.

See also selectLinkText, currentLinkUrl, and currentLinkText.