KRichTextWidget Class

A KRichTextEdit with common actions. More...

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

Public Types

flags RichTextSupport
enum RichTextSupportValues { DisableRichText, SupportBold, SupportItalic, SupportUnderline, SupportStrikeOut, …, FullSupport }

Public Functions

KRichTextWidget(QWidget *parent)
KRichTextWidget(const QString &text, QWidget *parent = nullptr)
virtual ~KRichTextWidget() override
(since 5.0) virtual QList<QAction *> createActions()
KRichTextWidget::RichTextSupport richTextSupport() const
void setRichTextSupport(const KRichTextWidget::RichTextSupport &support)
void updateActionStates()

Public Slots

void setActionsEnabled(bool enabled)

Detailed Description

This class implements common actions which are often used with KRichTextEdit. All you need to do is to call createActions(), and the actions will be added to your KXMLGUIWindow. Remember to also add the chosen actions to your application ui.rc file.

See the KRichTextWidget::RichTextSupportValues enum for an overview of supported actions.

"KRichTextWidget Widget"

Member Type Documentation

enum KRichTextWidget::RichTextSupportValues
flags KRichTextWidget::RichTextSupport

These flags describe what actions will be created by createActions() after passing a combination of these flags to setRichTextSupport().

ConstantValueDescription
KRichTextWidget::DisableRichText0x00No rich text support at all, no actions will be created. Do not use in combination with other flags.
KRichTextWidget::SupportBold0x01Action to format the selected text as bold. If no text is selected, the word under the cursor is formatted bold. This is a KToggleAction. The status is automatically updated when the text cursor is moved.
KRichTextWidget::SupportItalic0x02Action to format the selected text as italic. If no text is selected, the word under the cursor is formatted italic. This is a KToggleAction. The status is automatically updated when the text cursor is moved.
KRichTextWidget::SupportUnderline0x04Action to underline the selected text. If no text is selected, the word under the cursor is underlined. This is a KToggleAction. The status is automatically updated when the text cursor is moved.
KRichTextWidget::SupportStrikeOut0x08Action to strike out the selected text. If no text is selected, the word under the cursor is struck out. This is a KToggleAction. The status is automatically updated when the text cursor is moved.
KRichTextWidget::SupportFontFamily0x10Action to change the font family of the currently selected text. If no text is selected, the font family of the word under the cursor is changed. Displayed as a combobox when inserted into the toolbar. This is a KFontAction. The status is automatically updated when the text cursor is moved.
KRichTextWidget::SupportFontSize0x20Action to change the font size of the currently selected text. If no text is selected, the font size of the word under the cursor is changed. Displayed as a combobox when inserted into the toolbar. This is a KFontSizeAction. The status is automatically updated when the text cursor is moved.
KRichTextWidget::SupportTextForegroundColor0x40Action to change the text color of the currently selected text. If no text is selected, the text color of the word under the cursor is changed. Opens a QColorDialog to select the color.
KRichTextWidget::SupportTextBackgroundColor0x80Action to change the background color of the currently selected text. If no text is selected, the background color of the word under the cursor is changed. Opens a QColorDialog to select the color.
KRichTextWidget::FullTextFormattingSupport0xffA combination of all the flags above. Includes all actions that change the format of the text.
KRichTextWidget::SupportChangeListStyle0x100Action to make the current line a list element, change the list style or remove list formatting. Displayed as a combobox when inserted into a toolbar. This is a KSelectAction. The status is automatically updated when the text cursor is moved.
KRichTextWidget::SupportIndentLists0x200Action to increase the current list nesting level. This makes it possible to create nested lists.
KRichTextWidget::SupportDedentLists0x400Action to decrease the current list nesting level.
KRichTextWidget::FullListSupport0xf00All of the three list actions above. Includes all list-related actions.
KRichTextWidget::SupportAlignment0x100000Actions to align the current paragraph left, right, center or justify. These actions are KToogleActions. The status is automatically updated when the text cursor is moved.
KRichTextWidget::SupportRuleLine0x400000Action to insert a horizontal line.
KRichTextWidget::SupportHyperlinks0x800000Action to convert the current text to a hyperlink. If no text is selected, the word under the cursor is converted. This action opens a dialog where the user can enter the link target.
KRichTextWidget::SupportFormatPainting0x1000000Action to make the mouse cursor a format painter. The user can select text with that painter. The selected text gets the same format as the text that was previously selected.
KRichTextWidget::SupportToPlainText0x2000000Action to change the text of the whole text edit to plain text. All rich text formatting will get lost.
KRichTextWidget::SupportSuperScriptAndSubScript0x4000000Actions to format text as superscript or subscript. If no text is selected, the word under the cursor is formatted as selected. This is a KToggleAction. The status is automatically updated when the text cursor is moved.
KRichTextWidget::SupportDirection0x8000000Action to change direction of text to Right-To-Left or Left-To-Right.
KRichTextWidget::SupportHeading (since Qt 5.70)0x10000000Action to make the current line a heading (up to six levels, corresponding to HTML h1...h6 tags). Displayed as a combobox when inserted into a toolbar. This is a KSelectAction. The status is automatically updated when the text cursor is moved.
KRichTextWidget::FullSupport0xffffffffIncludes all above actions for full rich text support

The RichTextSupport type is a typedef for QFlags<RichTextSupportValues>. It stores an OR combination of RichTextSupportValues values.

Member Function Documentation

[explicit] KRichTextWidget::KRichTextWidget(QWidget *parent)

Constructor parent the parent widget

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

Constructs a KRichTextWidget object

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

parent The parent widget

[override virtual noexcept] KRichTextWidget::~KRichTextWidget()

Destructor

[virtual, since 5.0] QList<QAction *> KRichTextWidget::createActions()

Creates the actions and adds them to the given action collection.

Call this before calling setupGUI() in your application, but after calling setRichTextSupport().

The XML file of your KXmlGuiWindow needs to have the action names in them, so that the actions actually appear in the menu and in the toolbars.

Below is a list of actions that are created, depending on the supported rich text subset set by setRichTextSupport(). The list contains action names. Those names need to be the same in your XML file.

See the KRichTextWidget::RichTextSupportValues enum documentation for a detailed explanation of each action.

XML NameRichTextSupportValues flag
format_text_foreground_colorSupportTextForegroundColor
format_text_background_colorSupportTextBackgroundColor
format_font_familySupportFontFamily
format_font_sizeSupportFontSize
format_text_boldSupportBold
format_text_italicSupportItalic
format_text_underlineSupportUnderline
format_text_strikeoutSupportStrikeOut
format_align_leftSupportAlignment
format_align_centerSupportAlignment
format_align_rightSupportAlignment
format_align_justifySupportAlignment
direction_ltrSupportDirection
direction_rtlSupportDirection
format_list_styleSupportChangeListStyle
format_list_indent_moreSupportIndentLists
format_list_indent_lessSupportDedentLists
insert_horizontal_ruleSupportRuleLine
manage_linkSupportHyperlinks
format_painterSupportFormatPainting
action_to_plain_textSupportToPlainText
format_text_subscript & format_text_superscriptSupportSuperScriptAndSubScript
format_heading_levelSupportHeading

This function was introduced in 5.0.

KRichTextWidget::RichTextSupport KRichTextWidget::richTextSupport() const

Returns the supported rich text subset available. Returns The supported subset.

Note: Getter function for property richTextSupport.

See also setRichTextSupport().

[slot] void KRichTextWidget::setActionsEnabled(bool enabled)

Disables or enables all of the actions created by createActions(). This may be useful in cases where rich text mode may be set on or off.

enabled Whether to enable or disable the actions.

void KRichTextWidget::setRichTextSupport(const KRichTextWidget::RichTextSupport &support)

Sets the supported rich text subset available.

The default is KRichTextWidget::FullSupport and will be set in the constructor.

You need to call createActions() afterwards.

support The supported subset.

Note: Setter function for property richTextSupport.

See also richTextSupport().

void KRichTextWidget::updateActionStates()

Tells KRichTextWidget to update the state of the actions created by createActions(). This is normally automatically done, but there might be a few cases where you'll need to manually call this function.

Call this function only after calling createActions().