The QTextCharFormat class provides formatting information for characters in a QTextDocument. More...
Header: | #include <QTextCharFormat> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS Gui) target_link_libraries(mytarget PRIVATE Qt6::Gui) |
qmake: | QT += gui |
Inherits: | QTextFormat |
Inherited By: |
Note: All functions in this class are reentrant.
enum | FontPropertiesInheritanceBehavior { FontPropertiesSpecifiedOnly, FontPropertiesAll } |
enum | UnderlineStyle { NoUnderline, SingleUnderline, DashUnderline, DotLine, DashDotLine, …, SpellCheckUnderline } |
enum | VerticalAlignment { AlignNormal, AlignSuperScript, AlignSubScript, AlignMiddle, AlignBottom, …, AlignBaseline } |
QTextCharFormat() | |
QString | anchorHref() const |
QStringList | anchorNames() const |
qreal | baselineOffset() const |
QFont | font() const |
QFont::Capitalization | fontCapitalization() const |
QVariant | fontFamilies() const |
bool | fontFixedPitch() const |
QFont::HintingPreference | fontHintingPreference() const |
bool | fontItalic() const |
bool | fontKerning() const |
qreal | fontLetterSpacing() const |
QFont::SpacingType | fontLetterSpacingType() const |
bool | fontOverline() const |
qreal | fontPointSize() const |
int | fontStretch() const |
bool | fontStrikeOut() const |
QFont::StyleHint | fontStyleHint() const |
QVariant | fontStyleName() const |
QFont::StyleStrategy | fontStyleStrategy() const |
bool | fontUnderline() const |
int | fontWeight() const |
qreal | fontWordSpacing() const |
bool | isAnchor() const |
bool | isValid() const |
void | setAnchor(bool anchor) |
void | setAnchorHref(const QString &value) |
void | setAnchorNames(const QStringList &names) |
void | setBaselineOffset(qreal baseline) |
void | setFont(const QFont &font, QTextCharFormat::FontPropertiesInheritanceBehavior behavior = FontPropertiesAll) |
void | setFontCapitalization(QFont::Capitalization capitalization) |
void | setFontFamilies(const QStringList &families) |
void | setFontFixedPitch(bool fixedPitch) |
void | setFontHintingPreference(QFont::HintingPreference hintingPreference) |
void | setFontItalic(bool italic) |
void | setFontKerning(bool enable) |
void | setFontLetterSpacing(qreal spacing) |
void | setFontLetterSpacingType(QFont::SpacingType letterSpacingType) |
void | setFontOverline(bool overline) |
void | setFontPointSize(qreal size) |
void | setFontStretch(int factor) |
void | setFontStrikeOut(bool strikeOut) |
void | setFontStyleHint(QFont::StyleHint hint, QFont::StyleStrategy strategy = QFont::PreferDefault) |
void | setFontStyleName(const QString &styleName) |
void | setFontStyleStrategy(QFont::StyleStrategy strategy) |
void | setFontUnderline(bool underline) |
void | setFontWeight(int weight) |
void | setFontWordSpacing(qreal spacing) |
void | setSubScriptBaseline(qreal baseline) |
void | setSuperScriptBaseline(qreal baseline) |
void | setTextOutline(const QPen &pen) |
void | setToolTip(const QString &text) |
void | setUnderlineColor(const QColor &color) |
void | setUnderlineStyle(QTextCharFormat::UnderlineStyle style) |
void | setVerticalAlignment(QTextCharFormat::VerticalAlignment alignment) |
qreal | subScriptBaseline() const |
qreal | superScriptBaseline() const |
QPen | textOutline() const |
QString | toolTip() const |
QColor | underlineColor() const |
QTextCharFormat::UnderlineStyle | underlineStyle() const |
QTextCharFormat::VerticalAlignment | verticalAlignment() const |
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. setFontFamilies() 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.
[since 5.3]
enum QTextCharFormat::FontPropertiesInheritanceBehaviorThis enum specifies how the setFont() function should behave with respect to unset font properties.
Constant | Value | Description |
---|---|---|
QTextCharFormat::FontPropertiesSpecifiedOnly |
0 |
If a property is not explicitly set, do not change the text format's property value. |
QTextCharFormat::FontPropertiesAll |
1 |
If 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().
This enum describes the different ways drawing underlined text.
Constant | Value | Description |
---|---|---|
QTextCharFormat::NoUnderline |
0 |
Text is draw without any underlining decoration. |
QTextCharFormat::SingleUnderline |
1 |
A line is drawn using Qt::SolidLine. |
QTextCharFormat::DashUnderline |
2 |
Dashes are drawn using Qt::DashLine. |
QTextCharFormat::DotLine |
3 |
Dots are drawn using Qt::DotLine; |
QTextCharFormat::DashDotLine |
4 |
Dashes and dots are drawn using Qt::DashDotLine. |
QTextCharFormat::DashDotDotLine |
5 |
Underlines draw drawn using Qt::DashDotDotLine. |
QTextCharFormat::WaveUnderline |
6 |
The text is underlined using a wave shaped line. |
QTextCharFormat::SpellCheckUnderline |
7 |
The 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.
This enum describes the ways that adjacent characters can be vertically aligned.
Constant | Value | Description |
---|---|---|
QTextCharFormat::AlignNormal |
0 |
Adjacent characters are positioned in the standard way for text in the writing system in use. |
QTextCharFormat::AlignSuperScript |
1 |
Characters are placed above the base line for normal text. |
QTextCharFormat::AlignSubScript |
2 |
Characters are placed below the base line for normal text. |
QTextCharFormat::AlignMiddle |
3 |
The center of the object is vertically aligned with the base line. Currently, this is only implemented for inline objects. |
QTextCharFormat::AlignBottom |
5 |
The bottom edge of the object is vertically aligned with the base line. |
QTextCharFormat::AlignTop |
4 |
The top edge of the object is vertically aligned with the base line. |
QTextCharFormat::AlignBaseline |
6 |
The base lines of the characters are aligned. |
Constructs a new character format object.
Returns the text format's hypertext link, or an empty string if none has been set.
See also setAnchorHref().
Returns the anchor names associated with this text format, or an empty string list if none has been set. If the anchor names are set, text with this format can be the destination of a hypertext link.
See also setAnchorNames().
[since 6.0]
qreal QTextCharFormat::baselineOffset() constReturns the the baseline offset in %.
This function was introduced in Qt 6.0.
See also setBaselineOffset(), setSubScriptBaseline(), subScriptBaseline(), setSuperScriptBaseline(), and superScriptBaseline().
Returns the font for this character format.
See also setFont().
Returns the current capitalization type of the font.
See also setFontCapitalization().
[since 5.13]
QVariant QTextCharFormat::fontFamilies() constReturns the text format's font families.
Note: This function returns a QVariant for historical reasons. It will be corrected to return QStringList in Qt 7. The variant contains a QStringList object, which can be extracted by calling toStringList()
on it.
This function was introduced in Qt 5.13.
See also setFontFamilies() and font().
Returns true
if the text format's font is fixed pitch; otherwise returns false
.
See also setFontFixedPitch() and font().
Returns the hinting preference set for this text format.
See also setFontHintingPreference(), font(), and QFont::hintingPreference().
Returns true
if the text format's font is italic; otherwise returns false
.
See also setFontItalic() and font().
Returns true
if the font kerning is enabled.
See also setFontKerning() and font().
Returns the current letter spacing.
See also setFontLetterSpacing(), setFontLetterSpacingType(), and fontLetterSpacingType().
[since 5.0]
QFont::SpacingType QTextCharFormat::fontLetterSpacingType()
constReturns the letter spacing type of this format..
This function was introduced in Qt 5.0.
See also setFontLetterSpacingType(), setFontLetterSpacing(), and fontLetterSpacing().
Returns true
if the text format's font is overlined; otherwise returns false
.
See also setFontOverline() and font().
Returns the font size used to display text in this format.
See also setFontPointSize() and font().
[since 5.0]
int QTextCharFormat::fontStretch() constReturns the current font stretching.
This function was introduced in Qt 5.0.
See also setFontStretch().
Returns true
if the text format's font is struck out (has a horizontal line drawn through it); otherwise returns false
.
See also setFontStrikeOut() and font().
Returns the font style hint.
See also setFontStyleHint() and font().
[since 5.13]
QVariant QTextCharFormat::fontStyleName() constReturns the text format's font style name.
Note: This function returns a QVariant for historical reasons. It will be corrected to return QStringList in Qt 7. The variant contains a QStringList object, which can be extracted by calling toStringList()
on it.
This function was introduced in Qt 5.13.
See also setFontStyleName(), font(), and QFont::styleName().
Returns the current font style strategy.
See also setFontStyleStrategy() and font().
Returns true
if the text format's font is underlined; otherwise returns false
.
See also setFontUnderline() and font().
Returns the text format's font weight.
See also setFontWeight(), font(), and QFont::Weight.
Returns the current word spacing value.
See also setFontWordSpacing().
Returns true
if the text is formatted as an anchor; otherwise returns false
.
See also setAnchor(), setAnchorHref(), and setAnchorNames().
Returns true
if this character format is valid; otherwise returns false.
If anchor is true, text with this format represents an anchor, and is formatted in the appropriate way; otherwise the text is formatted normally. (Anchors are hyperlinks which are often shown underlined and in a different color from plain text.)
The way the text is rendered is independent of whether or not the format has a valid anchor defined. Use setAnchorHref(), and optionally setAnchorNames() to create a hypertext link.
See also isAnchor().
Sets the hypertext link for the text format to the given value. This is typically a URL like "http://example.com/index.html".
The anchor will be displayed with the value as its display text; if you want to display different text call setAnchorNames().
To format the text as a hypertext link use setAnchor().
See also anchorHref().
Sets the text format's anchor names. For the anchor to work as a hyperlink, the destination must be set with setAnchorHref() and the anchor must be enabled with setAnchor().
See also anchorNames().
[since 6.0]
void QTextCharFormat::setBaselineOffset(qreal baseline)Sets the base line (in % of height) of text to baseline. A positive value moves the text up, by the corresponding %; a negative value moves it down. The default value is 0.
This function was introduced in Qt 6.0.
See also baselineOffset(), setSubScriptBaseline(), subScriptBaseline(), setSuperScriptBaseline(), and superScriptBaseline().
[since 5.3]
void QTextCharFormat::setFont(const QFont &font,
QTextCharFormat::FontPropertiesInheritanceBehavior behavior = FontPropertiesAll)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().
Sets the capitalization of the text that appears in this font to capitalization.
A font's capitalization makes the text appear in the selected capitalization mode.
See also fontCapitalization().
[since 5.13]
void QTextCharFormat::setFontFamilies(const QStringList &families)Sets the text format's font families.
This function was introduced in Qt 5.13.
See also fontFamilies() and setFont().
If fixedPitch is true, sets the text format's font to be fixed pitch; otherwise a non-fixed pitch font is used.
See also fontFixedPitch() and setFont().
Sets the hinting preference of the text format's font to be hintingPreference.
See also fontHintingPreference(), setFont(), and QFont::setHintingPreference().
If italic is true, sets the text format's font to be italic; otherwise the font will be non-italic.
See also fontItalic() and setFont().
Enables kerning for this font if enable is true; otherwise disables it.
When kerning is enabled, glyph metrics do not add up anymore, even for Latin text. In other words, the assumption that width('a') + width('b') is equal to width("ab") is not neccesairly true.
See also fontKerning() and setFont().
Sets the letter spacing of this format to the given spacing. The meaning of the value depends on the font letter spacing type.
For percentage spacing a value of 100 indicates default spacing; a value of 200 doubles the amount of space a letter takes.
See also fontLetterSpacing(), setFontLetterSpacingType(), and fontLetterSpacingType().
[since 5.0]
void QTextCharFormat::setFontLetterSpacingType(QFont::SpacingType letterSpacingType)Sets the letter spacing type of this format to letterSpacingType.
This function was introduced in Qt 5.0.
See also fontLetterSpacingType(), setFontLetterSpacing(), and fontLetterSpacing().
If overline is true, sets the text format's font to be overlined; otherwise the font is displayed non-overlined.
See also fontOverline() and setFont().
Sets the text format's font size.
See also fontPointSize() and setFont().
[since 5.0]
void QTextCharFormat::setFontStretch(int factor)Sets the stretch factor for the font to factor.
The stretch factor changes the width of all characters in the font by factor percent. For example, setting factor to 150 results in all characters in the font being 1.5 times (ie. 150%) wider. The default stretch factor is 100. The minimum stretch factor is 1, and the maximum stretch factor is 4000.
The stretch factor is only applied to outline fonts. The stretch factor is ignored for bitmap fonts.
This function was introduced in Qt 5.0.
See also fontStretch().
If strikeOut is true, sets the text format's font with strike-out enabled (with a horizontal line through it); otherwise it is displayed without strikeout.
See also fontStrikeOut() and setFont().
Sets the font style hint and strategy.
Qt does not support style hints on X11 since this information is not provided by the window system.
See also fontStyleHint(), setFont(), and QFont::setStyleHint().
[since 5.13]
void QTextCharFormat::setFontStyleName(const QString
&styleName)Sets the text format's font styleName.
This function was introduced in Qt 5.13.
See also fontStyleName(), setFont(), and QFont::setStyleName().
Sets the font style strategy.
See also fontStyleStrategy(), setFont(), and QFont::setStyleStrategy().
If underline is true, sets the text format's font to be underlined; otherwise it is displayed non-underlined.
See also fontUnderline() and setFont().
Sets the text format's font weight to weight.
See also fontWeight(), setFont(), and QFont::Weight.
Sets the word spacing of this format to the given spacing, in pixels.
See also fontWordSpacing().
[since 6.0]
void QTextCharFormat::setSubScriptBaseline(qreal baseline)Sets the subscript's base line as a % of font height to baseline. The default value is 16.67% (1/6 of height)
This function was introduced in Qt 6.0.
See also subScriptBaseline(), setSuperScriptBaseline(), superScriptBaseline(), setBaselineOffset(), and baselineOffset().
[since 6.0]
void QTextCharFormat::setSuperScriptBaseline(qreal baseline)Sets the superscript's base line as a % of font height to baseline. The default value is 50% (1/2 of height).
This function was introduced in Qt 6.0.
See also superScriptBaseline(), setSubScriptBaseline(), subScriptBaseline(), setBaselineOffset(), and baselineOffset().
Sets the pen used to draw the outlines of characters to the given pen.
See also textOutline().
Sets the tool tip for a fragment of text to the given text.
See also toolTip().
Sets the color used to draw underlines, overlines and strikeouts on the characters with this format to the color specified.
See also underlineColor().
Sets the style of underlining the text to style.
See also underlineStyle().
Sets the vertical alignment used for the characters with this format to the alignment specified.
See also verticalAlignment().
[since 6.0]
qreal QTextCharFormat::subScriptBaseline() constReturns the subscript's base line as a % of font height.
This function was introduced in Qt 6.0.
See also setSubScriptBaseline(), setSuperScriptBaseline(), superScriptBaseline(), setBaselineOffset(), and baselineOffset().
[since 6.0]
qreal QTextCharFormat::superScriptBaseline() constReturns the superscript's base line as a % of font height.
This function was introduced in Qt 6.0.
See also setSuperScriptBaseline(), setSubScriptBaseline(), subScriptBaseline(), setBaselineOffset(), and baselineOffset().
Returns the pen used to draw the outlines of characters in this format.
See also setTextOutline().
Returns the tool tip that is displayed for a fragment of text.
See also setToolTip().
Returns the color used to draw underlines, overlines and strikeouts on the characters with this format.
See also setUnderlineColor().
Returns the style of underlining the text.
See also setUnderlineStyle().
Returns the vertical alignment used for characters with this format.
See also setVerticalAlignment().