QTextCharFormat Class

The QTextCharFormat class provides formatting information for characters in a QTextDocument. More...

Header: #include <QTextCharFormat>
qmake: QT += gui
Inherits: QTextFormat
Inherited By:

QTextImageFormat and QTextTableCellFormat

Note: All functions in this class are reentrant.

Public Types

enum FontPropertiesInheritanceBehavior { FontPropertiesSpecifiedOnly, FontPropertiesAll }
enum UnderlineStyle { NoUnderline, SingleUnderline, DashUnderline, DotLine, ..., SpellCheckUnderline }
enum VerticalAlignment { AlignNormal, AlignSuperScript, AlignSubScript, AlignMiddle, ..., AlignBaseline }

Public Functions

QFont font() const
void setFont(const QFont &font, QTextCharFormat::FontPropertiesInheritanceBehavior behavior)
void setFont(const QFont &font)

Detailed Description

The character format of text in a document specifies the visual properties of the text, as well as information about its role in a hypertext document.

The font used can be set by supplying a font to the setFont() function, and each aspect of its appearance can be adjusted to give the desired effect. setFontFamily() and setFontPointSize() define the font's family (e.g. Times) and printed size; setFontWeight() and setFontItalic() provide control over the style of the font. setFontUnderline(), setFontOverline(), setFontStrikeOut(), and setFontFixedPitch() provide additional effects for text.

The color is set with setForeground(). If the text is intended to be used as an anchor (for hyperlinks), this can be enabled with setAnchor(). The setAnchorHref() and setAnchorNames() functions are used to specify the information about the hyperlink's destination and the anchor's name.

See also QTextFormat, QTextBlockFormat, QTextTableFormat, and QTextListFormat.

Member Type Documentation

enum QTextCharFormat::FontPropertiesInheritanceBehavior

This enum specifies how the setFont() function should behave with respect to unset font properties.

ConstantValueDescription
QTextCharFormat::FontPropertiesSpecifiedOnly0If a property is not explicitly set, do not change the text format's property value.
QTextCharFormat::FontPropertiesAll1If a property is not explicitly set, override the text format's property with a default value.

This enum was introduced or modified in Qt 5.3.

See also setFont().

enum QTextCharFormat::UnderlineStyle

This enum describes the different ways drawing underlined text.

ConstantValueDescription
QTextCharFormat::NoUnderline0Text is draw without any underlining decoration.
QTextCharFormat::SingleUnderline1A line is drawn using Qt::SolidLine.
QTextCharFormat::DashUnderline2Dashes are drawn using Qt::DashLine.
QTextCharFormat::DotLine3Dots are drawn using Qt::DotLine;
QTextCharFormat::DashDotLine4Dashs and dots are drawn using Qt::DashDotLine.
QTextCharFormat::DashDotDotLine5Underlines draw drawn using Qt::DashDotDotLine.
QTextCharFormat::WaveUnderline6The text is underlined using a wave shaped line.
QTextCharFormat::SpellCheckUnderline7The underline is drawn depending on the SpellCheckUnderlineStyle theme hint of QPlatformTheme. By default this is mapped to WaveUnderline, on macOS it is mapped to DotLine.

See also Qt::PenStyle.

enum QTextCharFormat::VerticalAlignment

This enum describes the ways that adjacent characters can be vertically aligned.

ConstantValueDescription
QTextCharFormat::AlignNormal0Adjacent characters are positioned in the standard way for text in the writing system in use.
QTextCharFormat::AlignSuperScript1Characters are placed above the base line for normal text.
QTextCharFormat::AlignSubScript2Characters are placed below the base line for normal text.
QTextCharFormat::AlignMiddle3The center of the object is vertically aligned with the base line. Currently, this is only implemented for inline objects.
QTextCharFormat::AlignBottom5The bottom edge of the object is vertically aligned with the base line.
QTextCharFormat::AlignTop4The top edge of the object is vertically aligned with the base line.
QTextCharFormat::AlignBaseline6The base lines of the characters are aligned.

Member Function Documentation

QFont QTextCharFormat::font() const

Returns the font for this character format.

See also setFont().

void QTextCharFormat::setFont(const QFont &font, QTextCharFormat::FontPropertiesInheritanceBehavior behavior)

Sets the text format's font.

If behavior is QTextCharFormat::FontPropertiesAll, the font property that has not been explicitly set is treated like as it were set with default value; If behavior is QTextCharFormat::FontPropertiesSpecifiedOnly, the font property that has not been explicitly set is ignored and the respective property value remains unchanged.

This function was introduced in Qt 5.3.

See also font().

void QTextCharFormat::setFont(const QFont &font)

This is an overloaded function.

Sets the text format's font.

See also font().