PySide6.QtGui.QTextFormat

class QTextFormat

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

继承者: QTextListFormat, QTextFrameFormat, QTextTableFormat, QTextCharFormat, QTextTableCellFormat, QTextImageFormat, QTextBlockFormat

概要

方法

注意

本文档可能包含从C++自动翻译到Python的代码片段。我们始终欢迎对代码片段翻译的贡献。如果您发现翻译问题,您也可以通过在我们的https:/bugreports.qt.io/projects/PYSIDE上创建工单来告知我们。

详细描述

QTextFormat 是一个通用类,用于描述 QTextDocument 部分的格式。派生类 QTextCharFormatQTextBlockFormatQTextListFormatQTextTableFormat 通常更有用,它们描述了应用于文档特定部分的格式。

格式有一个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关联。它用于表示文档中的列表、框架和表格。

另请参阅

富文本处理

class FormatType

(继承自 enum.IntEnum) 此枚举描述了 QTextFormat 对象正在格式化的文本项。

常量

描述

QTextFormat.InvalidFormat

由默认构造函数创建的无效格式

QTextFormat.BlockFormat

该对象格式化一个文本块

QTextFormat.CharFormat

该对象格式化单个字符

QTextFormat.ListFormat

该对象格式化一个列表

QTextFormat.FrameFormat

该对象格式化一个框架

QTextFormat.UserFormat

class Property

(继承自 enum.IntEnum) 此枚举描述了格式可以具有的不同属性。

常量

描述

QTextFormat.ObjectIndex

格式化对象的索引。参见 objectIndex()

段落和字符属性

常量

描述

QTextFormat.CssFloat

框架相对于周围文本的定位方式

QTextFormat.LayoutDirection

文档中文本的布局方向(Qt::LayoutDirection)。

QTextFormat.OutlinePen

QTextFormat.ForegroundBrush

QTextFormat.BackgroundBrush

QTextFormat.BackgroundImageUrl

段落属性

常量

描述

QTextFormat.BlockAlignment

QTextFormat.BlockTopMargin

QTextFormat.BlockBottomMargin

QTextFormat.BlockLeftMargin

QTextFormat.BlockRightMargin

QTextFormat.TextIndent

QTextFormat.TabPositions

指定制表符位置。制表符位置是存储在QList中的Tab结构体(内部存储在QList中)。

QTextFormat.BlockIndent

QTextFormat.LineHeight

QTextFormat.LineHeightType

QTextFormat.BlockNonBreakableLines

QTextFormat.BlockTrailingHorizontalRulerWidth

水平标尺元素的宽度。

QTextFormat.HeadingLevel

标题的级别,例如1对应于HTML H1标签;否则为0。此枚举值已在Qt 5.12中添加。

QTextFormat.BlockCodeFence

用于Markdown代码块“围栏”的字符。如果代码块是缩进而不是围栏的,则该块不应具有此属性。此枚举值已在Qt 5.14中添加。

QTextFormat.BlockQuoteLevel

此块中嵌套引用的深度:1 表示该块是顶级块引用。不是块引用的块不应具有此属性。此枚举值已在 Qt 5.14 中添加。

QTextFormat.BlockCodeLanguage

预格式化或代码块中的编程语言。不包含代码的块不应具有此属性。此枚举值已在Qt 5.14中添加。

QTextFormat.BlockMarker

要显示在块旁边的type of adornment。此枚举值已在Qt 5.14中添加。

字符属性

常量

描述

QTextFormat.FontFamily

e{此属性已被弃用。} 请改用 QTextFormat::FontFamilies。

QTextFormat.FontFamilies

QTextFormat.FontStyleName

QTextFormat.FontPointSize

QTextFormat.FontPixelSize

QTextFormat.FontSizeAdjustment

指定使用FontPointSize或FontPixelSize设置的字体大小的变化。

QTextFormat.FontFixedPitch

QTextFormat.FontWeight

QTextFormat.FontItalic

QTextFormat.FontUnderline

此属性已被弃用。 请改用 QTextFormat::TextUnderlineStyle。

QTextFormat.FontOverline

QTextFormat.FontStrikeOut

QTextFormat.FontCapitalization

指定应用于文本的大写类型。

QTextFormat.FontLetterSpacingType

指定FontLetterSpacing属性的含义。默认值为PercentageSpacing

QTextFormat.FontLetterSpacing

更改字体中单个字母之间的默认间距。该值根据FontLetterSpacingType指定为百分比或绝对值。默认值为100%。

QTextFormat.FontWordSpacing

更改单个单词之间的默认间距。正值按相应像素增加单词间距;负值减少间距。

QTextFormat.FontStretch

对应于 Stretch 属性

QTextFormat.FontStyleHint

对应于 StyleHint 属性

QTextFormat.FontStyleStrategy

对应于 StyleStrategy 属性

QTextFormat.FontKerning

指定字体是否启用了字距调整。

QTextFormat.FontHintingPreference

根据HintingPreference枚举的值控制提示的使用。

QTextFormat.TextUnderlineColor

指定绘制下划线、上划线和删除线的颜色。

QTextFormat.TextVerticalAlignment

QTextFormat.TextOutline

QTextFormat.TextUnderlineStyle

QTextFormat.TextToolTip

指定要为文本片段显示的(可选)工具提示。

QTextFormat.TextSuperScriptBaseline

指定上标文本的基线(以高度的百分比表示)。

QTextFormat.TextSubScriptBaseline

指定下标文本的基线(以高度的百分比表示)。

QTextFormat.TextBaselineOffset

指定文本的基线(以高度的百分比表示)。正值将文本向上移动相应的百分比;负值将文本向下移动。

QTextFormat.IsAnchor

QTextFormat.AnchorHref

QTextFormat.AnchorName

QTextFormat.ObjectType

列出属性

常量

描述

QTextFormat.ListStyle

指定用于列表中项目的样式,由Style枚举的值描述。

QTextFormat.ListIndent

指定用于列表的缩进量。

QTextFormat.ListNumberPrefix

定义在数字列表中项目编号前添加的文本。

QTextFormat.ListNumberSuffix

定义在数字列表中附加到项目编号的文本。

QTextFormat.ListStart

定义列表的第一个值。

表格和框架属性

常量

描述

QTextFormat.FrameBorder

QTextFormat.FrameBorderBrush

QTextFormat.FrameBorderStyle

请参阅 BorderStyle 枚举。

QTextFormat.FrameBottomMargin

QTextFormat.FrameHeight

QTextFormat.FrameLeftMargin

QTextFormat.FrameMargin

QTextFormat.FramePadding

QTextFormat.FrameRightMargin

QTextFormat.FrameTopMargin

QTextFormat.FrameWidth

QTextFormat.TableCellSpacing

QTextFormat.TableCellPadding

QTextFormat.TableColumns

QTextFormat.TableColumnWidthConstraints

QTextFormat.TableHeaderRowCount

QTextFormat.TableBorderCollapse

指定 borderCollapse 属性。

表格单元格属性

常量

描述

QTextFormat.TableCellRowSpan

QTextFormat.TableCellColumnSpan

QTextFormat.TableCellLeftPadding

QTextFormat.TableCellRightPadding

QTextFormat.TableCellTopPadding

QTextFormat.TableCellBottomPadding

表格单元格属性,旨在与启用borderCollapse一起使用

常量

描述

QTextFormat.TableCellTopBorder

QTextFormat.TableCellBottomBorder

QTextFormat.TableCellLeftBorder

QTextFormat.TableCellRightBorder

QTextFormat.TableCellTopBorderStyle

QTextFormat.TableCellBottomBorderStyle

QTextFormat.TableCellLeftBorderStyle

QTextFormat.TableCellRightBorderStyle

QTextFormat.TableCellTopBorderBrush

QTextFormat.TableCellBottomBorderBrush

QTextFormat.TableCellLeftBorderBrush

QTextFormat.TableCellRightBorderBrush

图像属性

常量

描述

QTextFormat.ImageName

图像的文件名或来源。

QTextFormat.ImageTitle

HTML图像标签的title属性,或Markdown图像链接中URL后面的引号字符串。此枚举值已在Qt 5.14中添加。

QTextFormat.ImageAltText

HTML图像标签的alt属性,或Markdown图像链接中的图像描述。此枚举值已在Qt 5.14中添加。

QTextFormat.ImageWidth

QTextFormat.ImageHeight

QTextFormat.ImageQuality

QTextFormat.ImageMaxWidth

此枚举值已在 Qt 6.8 中添加。

选择属性

常量

描述

QTextFormat.FullWidthSelection

当在选择的字符格式上设置时,文本的整个宽度将显示为选中状态。

分页属性

常量

描述

QTextFormat.PageBreakPolicy

指定页面如何分页。请参阅PageBreakFlag枚举。

QTextFormat.UserProperty

另请参阅

property() setProperty()

class ObjectTypes

(继承自 enum.IntEnum) 此枚举描述了此格式与哪种 QTextObject 相关联。

常量

描述

QTextFormat.NoObject

QTextFormat.ImageObject

QTextFormat.TableObject

QTextFormat.TableCellObject

QTextFormat.UserObject

第一个可用于特定应用程序目的的对象。

class PageBreakFlag

(继承自 enum.Flag) 此枚举描述了打印时如何执行分页。它映射到相应的CSS属性。

常量

描述

QTextFormat.PageBreak_Auto

分页符根据当前页面的可用空间自动确定

QTextFormat.PageBreak_AlwaysBefore

在段落/表格之前总是分页

QTextFormat.PageBreak_AlwaysAfter

在段落/表格之后总是开始新的一页

__init__()

使用InvalidFormat创建一个新的文本格式。

另请参阅

FormatType

__init__(rhs)
Parameters:

rhsQTextFormat

创建一个与other文本格式具有相同属性的新文本格式。

__init__(type)
Parameters:

类型 – int

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

另请参阅

FormatType

background()
Return type:

QBrush

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

boolProperty(propertyId)
Parameters:

propertyId – int

Return type:

布尔

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

brushProperty(propertyId)
Parameters:

propertyId – int

Return type:

QBrush

返回由propertyId指定的属性的值;如果该属性不是QMetaType::QBrush类型,则返回Qt::NoBrush。

clearBackground()

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

clearForeground()

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

clearProperty(propertyId)
Parameters:

propertyId – int

清除由propertyId指定的属性的值

另请参阅

属性

colorProperty(propertyId)
Parameters:

propertyId – int

Return type:

QColor

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

doubleProperty(propertyId)
Parameters:

propertyId – int

Return type:

浮点数

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

foreground()
Return type:

QBrush

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

hasProperty(propertyId)
Parameters:

propertyId – int

Return type:

布尔

如果文本格式具有给定的propertyId属性,则返回true;否则返回false

另请参阅

properties() Property

intProperty(propertyId)
Parameters:

propertyId – int

Return type:

整数

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

isBlockFormat()
Return type:

布尔

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

isCharFormat()
Return type:

布尔

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

isEmpty()
Return type:

布尔

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

isFrameFormat()
Return type:

布尔

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

isImageFormat()
Return type:

布尔

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

isListFormat()
Return type:

布尔

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

isTableCellFormat()
Return type:

布尔

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

isTableFormat()
Return type:

布尔

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

isValid()
Return type:

布尔

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

layoutDirection()
Return type:

LayoutDirection

返回文档的布局方向。

另请参阅

setLayoutDirection()

lengthProperty(propertyId)
Parameters:

propertyId – int

Return type:

QTextLength

返回由propertyId指定的属性的值。

lengthVectorProperty(propertyId)
Parameters:

propertyId – int

Return type:

QTextLength的列表

返回由propertyId指定的属性的值。如果该属性不是QTextFormat::LengthVector类型,则返回一个空列表。

merge(other)
Parameters:

其他QTextFormat

other格式与此格式合并;在存在冲突的情况下,other格式优先。

objectIndex()
Return type:

整数

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

另请参阅

setObjectIndex()

objectType()
Return type:

整数

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

__ne__(rhs)
Parameters:

rhsQTextFormat

Return type:

布尔

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

__eq__(rhs)
Parameters:

rhsQTextFormat

Return type:

布尔

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

penProperty(propertyId)
Parameters:

propertyId – int

Return type:

QPen

返回由propertyId指定的属性的值;如果该属性不是QMetaType::QPen类型,则返回Qt::NoPen。

properties()
Return type:

字典的键类型为 .int,值类型为 QVariant。

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

property(propertyId)
Parameters:

propertyId – int

Return type:

对象

返回由给定的propertyId指定的属性。

另请参阅

setProperty() Property

propertyCount()
Return type:

整数

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

setBackground(brush)
Parameters:

画笔QBrush

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

setForeground(brush)
Parameters:

画笔QBrush

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

setLayoutDirection(direction)
Parameters:

方向LayoutDirection

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

另请参阅

layoutDirection()

setObjectIndex(object)
Parameters:

对象 – 整数

设置格式对象的对象 index

另请参阅

objectIndex()

setObjectType(type)
Parameters:

类型 – int

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

另请参阅

ObjectTypes objectType()

setProperty(propertyId, lengths)
Parameters:
  • propertyId – int

  • lengths – QTextLength 的列表

propertyId指定的属性值设置为value

setProperty(propertyId, value)
Parameters:
  • propertyId – int

  • value – 对象

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

另请参阅

属性

stringProperty(propertyId)
Parameters:

propertyId – int

Return type:

字符串

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

swap(other)
Parameters:

其他QTextFormat

将此文本格式与other交换。此函数非常快且永远不会失败。

toBlockFormat()
Return type:

QTextBlockFormat

将此格式返回为块格式。

toCharFormat()
Return type:

QTextCharFormat

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

toFrameFormat()
Return type:

QTextFrameFormat

将此格式返回为帧格式。

toImageFormat()
Return type:

QTextImageFormat

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

toListFormat()
Return type:

QTextListFormat

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

toTableCellFormat()
Return type:

QTextTableCellFormat

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

toTableFormat()
Return type:

QTextTableFormat

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

type()
Return type:

整数

返回此格式的类型。

另请参阅

FormatType