QTextFormat Class

QTextFormat 类为QTextDocument...提供格式化信息。

头文件: #include <QTextFormat>
CMake: find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
qmake: QT += gui
继承者:

QTextBlockFormat,QTextCharFormat,QTextFrameFormat, 以及QTextListFormat

注意:该类中的所有函数都是可重入的

公共类型

enum FormatType { InvalidFormat, BlockFormat, CharFormat, ListFormat, FrameFormat, UserFormat }
enum ObjectTypes { NoObject, ImageObject, TableObject, TableCellObject, UserObject }
enum PageBreakFlag { PageBreak_Auto, PageBreak_AlwaysBefore, PageBreak_AlwaysAfter }
flags PageBreakFlags
enum Property { ObjectIndex, CssFloat, LayoutDirection, OutlinePen, ForegroundBrush, …, UserProperty }

公共函数

QTextFormat()
QTextFormat(int type)
QTextFormat(const QTextFormat &other)
~QTextFormat()
QBrush background() const
bool boolProperty(int propertyId) const
QBrush brushProperty(int propertyId) const
void clearBackground()
void clearForeground()
void clearProperty(int propertyId)
QColor colorProperty(int propertyId) const
qreal doubleProperty(int propertyId) const
QBrush foreground() const
bool hasProperty(int propertyId) const
int intProperty(int propertyId) const
bool isBlockFormat() const
bool isCharFormat() const
bool isEmpty() const
bool isFrameFormat() const
bool isImageFormat() const
bool isListFormat() const
bool isTableCellFormat() const
bool isTableFormat() const
bool isValid() const
Qt::LayoutDirection layoutDirection() const
QTextLength lengthProperty(int propertyId) const
QList<QTextLength> lengthVectorProperty(int propertyId) const
void merge(const QTextFormat &other)
int objectIndex() const
int objectType() const
QPen penProperty(int propertyId) const
QMap<int, QVariant> properties() const
QVariant property(int propertyId) const
int propertyCount() const
void setBackground(const QBrush &brush)
void setForeground(const QBrush &brush)
void setLayoutDirection(Qt::LayoutDirection direction)
void setObjectIndex(int index)
void setObjectType(int type)
void setProperty(int propertyId, const QList<QTextLength> &value)
void setProperty(int propertyId, const QVariant &value)
QString stringProperty(int propertyId) const
void swap(QTextFormat &other)
QTextBlockFormat toBlockFormat() const
QTextCharFormat toCharFormat() const
QTextFrameFormat toFrameFormat() const
QTextImageFormat toImageFormat() const
QTextListFormat toListFormat() const
QTextTableCellFormat toTableCellFormat() const
QTextTableFormat toTableFormat() const
int type() const
QVariant operator QVariant() const
bool operator!=(const QTextFormat &other) const
QTextFormat &operator=(const QTextFormat &other)
bool operator==(const QTextFormat &other) const

详细说明

QTextFormat 是一个通用类,用于描述QTextDocument 中各部分的格式。派生类QTextCharFormat,QTextBlockFormat,QTextListFormat, 和QTextTableFormat 通常更有用,它们描述了应用于文档特定部分的格式。

格式有一个FormatType ,它指定了格式化文本项的种类,如文本块、列表、表格等。格式还有各种属性(有些是特定格式类型的特有属性),这些属性由属性枚举描述。每个属性都有一个对应的属性。

格式类型由type() 给出,格式可以通过isCharFormat(),isBlockFormat(),isListFormat(),isTableFormat(),isFrameFormat() 和isImageFormat() 进行测试。如果类型已确定,则可使用toCharFormat(),toBlockFormat(),toListFormat(),toTableFormat(),toFrameFormat(), 和toImageFormat() 进行检索。

格式的属性可以用setProperty() 函数设置,也可以用boolProperty() 、intProperty() 、doubleProperty() 和stringProperty() 检索。使用 allPropertyIds() 可以检索格式中使用的所有属性 ID。可以使用merge() 将一种格式合并到另一种格式中。

格式的对象索引可以用setObjectIndex() 设置,也可以用objectIndex() 检索。这些方法可用于将格式与QTextObject 关联起来。它可用于在文档中表示列表、框架和表格。

另请参阅 富文本处理

成员类型文档

enum QTextFormat::FormatType

该枚举描述QTextFormat 对象正在格式化的文本项目。

常量说明
QTextFormat::InvalidFormat-1默认构造函数创建的无效格式
QTextFormat::BlockFormat1对象格式为文本块
QTextFormat::CharFormat2对象格式为单个字符
QTextFormat::ListFormat3对象格式为列表
QTextFormat::FrameFormat5对象格式为帧
QTextFormat::UserFormat100

另请参阅 QTextCharFormat,QTextBlockFormat,QTextListFormat,QTextTableFormattype() 。

enum QTextFormat::ObjectTypes

该枚举描述了该格式与哪种QTextObject 关联。

常量描述
QTextFormat::NoObject0
QTextFormat::ImageObject1
QTextFormat::TableObject2
QTextFormat::TableCellObject3
QTextFormat::UserObject0x1000第一个可用于特定应用目的的对象。

另请参阅 QTextObject,QTextTable, 和QTextObject::format() 。

枚举 QTextFormat::PageBreakFlag
flags QTextFormat::PageBreakFlags

该枚举描述了打印时如何进行分页。它映射到相应的 css 属性。

常量说明
QTextFormat::PageBreak_Auto0自动分页取决于当前页面的可用空间
QTextFormat::PageBreak_AlwaysBefore0x001页面总是在段落/表格之前分页
QTextFormat::PageBreak_AlwaysAfter0x010新页面总是在段落/表格之后开始

PageBreakFlags 类型是QFlags<PageBreakFlag> 的类型定义。它存储 PageBreakFlag 值的 OR 组合。

另请参阅 QTextBlockFormat::pageBreakPolicy(),QTextFrameFormat::pageBreakPolicy() 和PageBreakPolicy

enum QTextFormat::Property

该枚举描述了格式可能具有的不同属性。

常量说明
QTextFormat::ObjectIndex0x0格式化对象的索引。请参见objectIndex()。

段落和字符属性

常数说明
QTextFormat::CssFloat0x0800框架相对于周围文本的位置
QTextFormat::LayoutDirection0x0801文档中文本的布局方向 (Qt::LayoutDirection)。
QTextFormat::OutlinePen0x810
QTextFormat::ForegroundBrush0x821
QTextFormat::BackgroundBrush0x820
QTextFormat::BackgroundImageUrl0x823

段落属性

常量说明
QTextFormat::BlockAlignment0x1010
QTextFormat::BlockTopMargin0x1030
QTextFormat::BlockBottomMargin0x1031
QTextFormat::BlockLeftMargin0x1032
QTextFormat::BlockRightMargin0x1033
QTextFormat::TextIndent0x1034
QTextFormat::TabPositions0x1035指定制表符位置。制表符位置是QTextOption::Tab 的结构,存储在QList 中(内部存储在QList<QVariant> 中)。
QTextFormat::BlockIndent0x1040
QTextFormat::LineHeight0x1048
QTextFormat::LineHeightType0x1049
QTextFormat::BlockNonBreakableLines0x1050
QTextFormat::BlockTrailingHorizontalRulerWidth0x1060水平标尺元素的宽度。
QTextFormat::HeadingLevel0x1070标题的级别,例如 1 对应 HTML H1 标记;否则为 0。该枚举值已在 Qt 5.12 中添加。
QTextFormat::BlockCodeFence0x1091Markdown 代码块周围的 "栅栏 "中使用的字符。如果代码块是缩进而非栅栏,则该代码块不应具有此属性。Qt 5.14 中添加了此枚举值。
QTextFormat::BlockQuoteLevel0x1080该代码块上嵌套引号的深度:1 表示该代码块是顶级块引号。不是分块引号的块不应具有此属性。此枚举值已在 Qt 5.14 中添加。
QTextFormat::BlockCodeLanguage0x1090预格式块或代码块中的编程语言。不包含代码的块不应具有此属性。该枚举值已在 Qt 5.14 中添加。
QTextFormat::BlockMarker0x10A0块旁边显示的type of adornment 。此枚举值已在 Qt 5.14 中添加。

字符属性

常量说明
QTextFormat::FontFamily0x2000e{此属性已被弃用}。请使用 QTextFormat::FontFamilies 代替。
QTextFormat::FontFamilies0x1FE7
QTextFormat::FontStyleName0x1FE8
QTextFormat::FontPointSize0x2001
QTextFormat::FontPixelSize0x2009
QTextFormat::FontSizeAdjustment0x2002指定已使用 FontPointSize 或 FontPixelSize 设置的字体大小的变化。
QTextFormat::FontFixedPitch0x2008
QTextFormat::FontWeight0x2003
QTextFormat::FontItalic0x2004
QTextFormat::FontUnderline0x2005该属性已被弃用。请使用 QTextFormat::TextUnderlineStyle 代替。
QTextFormat::FontOverline0x2006
QTextFormat::FontStrikeOut0x2007
QTextFormat::FontCapitalizationFirstFontProperty指定应用于文本的大写类型。
QTextFormat::FontLetterSpacingType0x1FE9指定 FontLetterSpacing 属性的含义。默认值为QFont::PercentageSpacing
QTextFormat::FontLetterSpacing0x1FE1更改字体中单个字母之间的默认间距。根据 FontLetterSpacingType,该值可指定为百分比或绝对值。默认值为 100%。
QTextFormat::FontWordSpacing0x1FE2更改单词之间的默认间距。正值增加相应像素的字间距,负值减小字间距。
QTextFormat::FontStretch0x1FEA对应于QFont::Stretch 属性
QTextFormat::FontStyleHint0x1FE3对应于QFont::StyleHint 属性
QTextFormat::FontStyleStrategy0x1FE4对应于QFont::StyleStrategy 属性
QTextFormat::FontKerning0x1FE5指定字体是否已打开字距调整。
QTextFormat::FontHintingPreference0x1FE6根据QFont::HintingPreference 枚举的值控制提示的使用。
QTextFormat::TextUnderlineColor0x2020指定绘制下划线、上划线和删除线的颜色。
QTextFormat::TextVerticalAlignment0x2021根据QTextCharFormat::VerticalAlignment 枚举的值指定文本垂直对齐方式。
QTextFormat::TextOutline0x2022指定用于绘制文本轮廓的QPen
QTextFormat::TextUnderlineStyle0x2023根据QTextCharFormat::UnderlineStyle 枚举的值指定文本下划线的样式。
QTextFormat::TextToolTip0x2024为文本片段指定要显示的工具提示(可选)。
QTextFormat::TextSuperScriptBaseline0x2025指定上标文本的基线(高度的百分比)。
QTextFormat::TextSubScriptBaseline0x2026指定下标文本的基线(高度的百分比)。
QTextFormat::TextBaselineOffset0x2027指定文本的基线(高度的百分比)。正值按相应的百分比向上移动文本,负值则向下移动文本。
QTextFormat::IsAnchor0x2030
QTextFormat::AnchorHref0x2031
QTextFormat::AnchorName0x2032
QTextFormat::ObjectType0x2f00

列表属性

常数说明
QTextFormat::ListStyle0x3000指定列表中项目的样式,由QTextListFormat::Style 枚举的值描述。
QTextFormat::ListIndent0x3001指定列表的缩进量。
QTextFormat::ListNumberPrefix0x3002定义数字列表中项目编号的前置文本。
QTextFormat::ListNumberSuffix0x3003定义附加到数字列表中项号的文本。
QTextFormat::ListStart (since Qt 6.6)0x3004定义列表的第一个值。

表格和框架属性

常数说明
QTextFormat::FrameBorder0x4000
QTextFormat::FrameBorderBrush0x4009
QTextFormat::FrameBorderStyle0x4010请参阅BorderStyle 枚举。
QTextFormat::FrameBottomMargin0x4006
QTextFormat::FrameHeight0x4004
QTextFormat::FrameLeftMargin0x4007
QTextFormat::FrameMargin0x4001
QTextFormat::FramePadding0x4002
QTextFormat::FrameRightMargin0x4008
QTextFormat::FrameTopMargin0x4005
QTextFormat::FrameWidth0x4003
QTextFormat::TableCellSpacing0x4102
QTextFormat::TableCellPadding0x4103
QTextFormat::TableColumns0x4100
QTextFormat::TableColumnWidthConstraints0x4101
QTextFormat::TableHeaderRowCount0x4104
QTextFormat::TableBorderCollapse0x4105指定QTextTableFormat::borderCollapse 属性。

表格单元格属性

常数
QTextFormat::TableCellRowSpan0x4810
QTextFormat::TableCellColumnSpan0x4811
QTextFormat::TableCellLeftPadding0x4814
QTextFormat::TableCellRightPadding0x4815
QTextFormat::TableCellTopPadding0x4812
QTextFormat::TableCellBottomPadding0x4813

启用QTextTableFormat::borderCollapse 时使用的表格单元格属性

常量
QTextFormat::TableCellTopBorder0x4816
QTextFormat::TableCellBottomBorder0x4817
QTextFormat::TableCellLeftBorder0x4818
QTextFormat::TableCellRightBorder0x4819
QTextFormat::TableCellTopBorderStyle0x481a
QTextFormat::TableCellBottomBorderStyle0x481b
QTextFormat::TableCellLeftBorderStyle0x481c
QTextFormat::TableCellRightBorderStyle0x481d
QTextFormat::TableCellTopBorderBrush0x481e
QTextFormat::TableCellBottomBorderBrush0x481f
QTextFormat::TableCellLeftBorderBrush0x4820
QTextFormat::TableCellRightBorderBrush0x4821

图像属性

常量描述
QTextFormat::ImageName0x5000图片的文件名或来源。
QTextFormat::ImageTitle0x5001HTML 图像标记的标题属性,或 Markdown 图像链接中 URL 后的引号字符串。此枚举值已在 Qt 5.14 中添加。
QTextFormat::ImageAltText0x5002HTML 图像标记的 alt 属性,或 Markdown 图像链接中的图像描述。此枚举值已在 Qt 5.14 中添加。
QTextFormat::ImageWidth0x5010
QTextFormat::ImageHeight0x5011
QTextFormat::ImageQuality0x5014
QTextFormat::ImageMaxWidth0x5015此枚举值已在 Qt 6.8 中添加。

选择属性

常量说明
QTextFormat::FullWidthSelection0x06000当在选区的 characterFormat 上设置时,将显示选中文本的整个宽度。

分页符属性

常数说明
QTextFormat::PageBreakPolicy0x7000指定分页方式。请参阅PageBreakFlag 枚举。
QTextFormat::UserProperty0x100000

另请参阅 property() 和setProperty()。

成员函数文档

QTextFormat::QTextFormat()

创建一种新的文本格式,其InvalidFormat

另请参阅 FormatType

[explicit] QTextFormat::QTextFormat(int type)

以给定的type 格式创建新文本格式。

另请参阅 FormatType

QTextFormat::QTextFormat(const QTextFormat &other)

创建与other 文本格式属性相同的新文本格式。

[noexcept] QTextFormat::~QTextFormat()

销毁此文本格式。

QBrush QTextFormat::background() const

返回用于绘制文档背景的笔刷。

另请参阅 setBackground()、clearBackground() 和foreground()。

bool QTextFormat::boolProperty(int propertyId) const

返回propertyId 指定的属性值。如果属性不是 QTextFormat::Bool 类型,则返回 false。

另请参阅 setProperty(),intProperty(),doubleProperty(),stringProperty(),colorProperty(),lengthProperty(),lengthVectorProperty(), 和Property

QBrush QTextFormat::brushProperty(int propertyId) const

返回propertyId 所给属性的值;如果该属性不是QMetaType::QBrush 类型,则返回Qt::NoBrush

另请参阅 setProperty(),boolProperty(),intProperty(),doubleProperty(),stringProperty(),lengthProperty(),lengthVectorProperty() 和Property

void QTextFormat::clearBackground()

清除用于绘制文档背景的笔刷。将使用默认笔刷。

另请参阅 background()、setBackground() 和clearForeground()。

void QTextFormat::clearForeground()

清除用于绘制文档前景的笔刷。将使用默认笔刷。

另请参阅 foreground()、setForeground() 和clearBackground()。

void QTextFormat::clearProperty(int propertyId)

清除由propertyId

另请参见 Property

QColor QTextFormat::colorProperty(int propertyId) const

返回propertyId 所给属性的值;如果属性不是QMetaType::QColor 类型,则返回无效颜色。

另请参阅 setProperty(),boolProperty(),intProperty(),doubleProperty(),stringProperty(),lengthProperty(),lengthVectorProperty(), 和Property

qreal QTextFormat::doubleProperty(int propertyId) const

返回propertyId 指定的属性值。如果属性不是QMetaType::DoubleQMetaType::Float 类型,则返回 0。

另请参阅 setProperty(),boolProperty(),intProperty(),stringProperty(),colorProperty(),lengthProperty(),lengthVectorProperty(), 和Property

QBrush QTextFormat::foreground() const

返回用于渲染前景细节(如文本、框架轮廓和表格边框)的笔刷。

另请参阅 setForeground()、clearForeground() 和background()。

bool QTextFormat::hasProperty(int propertyId) const

如果文本格式的属性为propertyId ,则返回true ;否则返回false

另请参阅 properties() 和Property

int QTextFormat::intProperty(int propertyId) const

返回propertyId 指定的属性值。如果属性不是 QTextFormat::Integer 类型,则返回 0。

另请参阅 setProperty(),boolProperty(),doubleProperty(),stringProperty(),colorProperty(),lengthProperty(),lengthVectorProperty(), 和Property

bool QTextFormat::isBlockFormat() const

如果该文本格式是BlockFormat ,则返回true ;否则返回false

bool QTextFormat::isCharFormat() const

如果该文本格式是CharFormat ,则返回true ;否则返回false

bool QTextFormat::isEmpty() const

如果格式不存储任何属性,则返回 true;否则返回 false。

另请参阅 propertyCount() 和properties()。

bool QTextFormat::isFrameFormat() const

如果该文本格式是FrameFormat ,则返回true ;否则返回false

bool QTextFormat::isImageFormat() const

如果该文本格式是图像格式,则返回true ;否则返回false

bool QTextFormat::isListFormat() const

如果该文本格式是ListFormat ,则返回true ;否则返回false

bool QTextFormat::isTableCellFormat() const

如果该文本格式是TableCellFormat ,则返回true ;否则返回false

bool QTextFormat::isTableFormat() const

如果该文本格式是TableFormat ,则返回true ;否则返回false

bool QTextFormat::isValid() const

如果格式有效(即不是InvalidFormat ),则返回true ;否则返回false

Qt::LayoutDirection QTextFormat::layoutDirection() const

返回文档的布局方向。

另请参阅 setLayoutDirection()。

QTextLength QTextFormat::lengthProperty(int propertyId) const

返回propertyId 所给属性的值。

另请参阅 setProperty(),boolProperty(),intProperty(),doubleProperty(),stringProperty(),colorProperty(),lengthVectorProperty(), 和Property

QList<QTextLength> QTextFormat::lengthVectorProperty(int propertyId) const

返回propertyId 给定的属性值。如果属性不是 QTextFormat::LengthVector 类型,则返回空列表。

另请参阅 setProperty(),boolProperty(),intProperty(),doubleProperty(),stringProperty(),colorProperty(),lengthProperty(), 和Property

void QTextFormat::merge(const QTextFormat &other)

other 格式与此格式合并;如有冲突,以other 格式为准。

int QTextFormat::objectIndex() const

返回格式对象的索引,如果格式对象无效,则返回-1。

另请参见 setObjectIndex().

int QTextFormat::objectType() const

返回文本格式的对象类型。

另请参阅 ObjectTypessetObjectType()。

QPen QTextFormat::penProperty(int propertyId) const

返回propertyId 所给属性的值;如果该属性不是QMetaType::QPen 类型,则返回Qt::NoPen

另请参阅 setProperty(),boolProperty(),intProperty(),doubleProperty(),stringProperty(),lengthProperty(),lengthVectorProperty() 和Property

QMap<int, QVariant> QTextFormat::properties() const

返回包含此文本格式所有属性的映射。

QVariant QTextFormat::property(int propertyId) const

返回给定propertyId 指定的属性。

另请参阅 setProperty() 和Property

int QTextFormat::propertyCount() const

返回格式中存储的属性数量。

void QTextFormat::setBackground(const QBrush &brush)

将用于绘制文档背景的笔刷设置为指定的brush

另请参阅 background()、clearBackground() 和setForeground()。

void QTextFormat::setForeground(const QBrush &brush)

将前景画笔设置为指定的brush 。前景画笔主要用于渲染文本。

另请参阅 foreground()、clearForeground() 和setBackground()。

void QTextFormat::setLayoutDirection(Qt::LayoutDirection direction)

将文档的布局方向设置为指定的direction

另请参阅 layoutDirection() 。

void QTextFormat::setObjectIndex(int index)

设置格式对象的对象index

另请参阅 objectIndex() 。

void QTextFormat::setObjectType(int type)

将文本格式的对象类型设置为type

另请参阅 ObjectTypesobjectType()。

void QTextFormat::setProperty(int propertyId, const QList<QTextLength> &value)

propertyId 给定的属性值设置为value

另请参阅 property(),lengthVectorProperty() 和Property

void QTextFormat::setProperty(int propertyId, const QVariant &value)

propertyId 指定的属性设置为给定的value

另请参阅 Property

QString QTextFormat::stringProperty(int propertyId) const

返回propertyId 给定的属性值;如果属性不是QMetaType::QString 类型,则返回空字符串。

另请参阅 setProperty(),boolProperty(),intProperty(),doubleProperty(),colorProperty(),lengthProperty(),lengthVectorProperty() 和Property

void QTextFormat::swap(QTextFormat &other)

将此文本格式与other 互换。该操作速度非常快,从未出现过故障。

QTextBlockFormat QTextFormat::toBlockFormat() const

以块格式返回该格式。

QTextCharFormat QTextFormat::toCharFormat() const

将此格式作为字符格式返回。

QTextFrameFormat QTextFormat::toFrameFormat() const

将此格式作为帧格式返回。

QTextImageFormat QTextFormat::toImageFormat() const

将此格式作为图像格式返回。

QTextListFormat QTextFormat::toListFormat() const

将此格式作为列表格式返回。

QTextTableCellFormat QTextFormat::toTableCellFormat() const

将此格式作为表格单元格格式返回。

QTextTableFormat QTextFormat::toTableFormat() const

将此格式作为表格格式返回。

int QTextFormat::type() const

返回此格式的类型。

另请参见 FormatType

QVariant QTextFormat::operator QVariant() const

QVariant

bool QTextFormat::operator!=(const QTextFormat &other) const

如果该文本格式与other 文本格式不同,则返回true

QTextFormat &QTextFormat::operator=(const QTextFormat &other)

other 文本格式赋值给该文本格式,并返回对该文本格式的引用。

bool QTextFormat::operator==(const QTextFormat &other) const

如果此文本格式与other 文本格式相同,则返回true

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.