本页内容

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
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 ),用于指定其可格式化的文本项类型;例如:文本块、列表、表格等。格式还具有各种属性(其中部分属性仅适用于特定格式类型),这些属性由Property枚举定义。每个属性都有对应的Property成员。

格式类型由type()函数确定,可通过isCharFormat()、isBlockFormat()、isListFormat()、isTableFormat()、isFrameFormat()和isImageFormat()函数对格式进行检测。若已确定类型,则可通过toCharFormat()、toBlockFormat()、toListFormat()、toTableFormat()、toFrameFormat()和toImageFormat()函数获取该类型。

可以通过setProperty() 函数设置格式的属性,并根据需要使用boolProperty()、intProperty()、doubleProperty() 和stringProperty() 函数进行获取。所有在该格式中使用的属性 ID 均可通过 allPropertyIds() 获取。可以使用merge() 将一种格式合并到另一种格式中。

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

另请参阅 “富文本处理”。

成员类型文档

enum QTextFormat::FormatType

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

常量值描述
QTextFormat::InvalidFormat-1由默认构造函数创建的格式无效
QTextFormat::BlockFormat1该对象正在对一个文本块进行格式化
QTextFormat::CharFormat2该对象对单个字符进行格式化
QTextFormat::ListFormat3该对象对列表进行格式化
QTextFormat::FrameFormat5该对象对一个框架进行格式化
QTextFormat::UserFormat100 

另请参阅 QTextCharFormat 、QTextBlockFormat 、QTextListFormat 、QTextTableFormat 以及type()。

enum QTextFormat::ObjectTypes

此枚举描述了该格式关联的QTextObject 类型。

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

另请参阅 QTextObject 、QTextTable 以及QTextObject::format()。

enum QTextFormat::PageBreakFlag
flags QTextFormat::PageBreakFlags

此枚举描述了打印时分页的方式,并映射到相应的 CSS 属性。

常量值描述
QTextFormat::PageBreak_Auto0分页位置将根据当前页面的可用空间自动确定
QTextFormat::PageBreak_AlwaysBefore0x001页面总是在段落/表格之前换行
QTextFormat::PageBreak_AlwaysAfter0x010段落/表格之后总是开始新页面

PageBreakFlags 类型是QFlags<PageBreakFlag> 的 typedef。它存储 PageBreakFlag 值的按“或”运算组合。

另请参阅 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::FontFeatures0x2010[自 6.11 起] 为排版特征分配整数值。有关详细信息,请参阅QFont::setFeature()。
QTextFormat::FontVariableAxes0x2011[自 6.11 起] 为可变字体的可变轴分配浮点数。有关详细信息,请参阅QFont::setVariableAxis()。
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 图片标签的 title 属性,或 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::Double 或QMetaType::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

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

另请参阅 ` ObjectTypes ` 和 `setObjectType()`。

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 。

另请参阅 ObjectTypes 和objectType()。

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 。

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 。

© 2026 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.