QTextFormat 类

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 QVariant & value )
void setProperty (int propertyId , const QList<QTextLength> & 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 which specifies the kinds of text item it can format; e.g. a block of text, a list, a table, etc. A format also has various properties (some specific to particular format types), as described by the Property enum. Every property has a corresponding Property.

The format type is given by type (), and the format can be tested with isCharFormat (), isBlockFormat (), isListFormat (), isTableFormat (), isFrameFormat (),和 isImageFormat (). If the type is determined, it can be retrieved with toCharFormat (), toBlockFormat (), toListFormat (), toTableFormat (), toFrameFormat (),和 toImageFormat ().

A format's properties can be set with the setProperty () functions, and retrieved with boolProperty (), intProperty (), doubleProperty (),和 stringProperty () as appropriate. All the property IDs used in the format can be retrieved with allPropertyIds(). One format can be merged into another using merge ().

A format's object index can be set with setObjectIndex (),和检索采有 objectIndex (). These methods can be used to associate the format with a QTextObject . It is used to represent lists, frames, and tables inside the document.

另请参阅 富文本处理 .

成员类型文档编制

enum QTextFormat:: FormatType

This enum describes the text item a QTextFormat object is formatting.

常量 描述
QTextFormat::InvalidFormat -1 由默认构造函数创建的无效格式
QTextFormat::BlockFormat 1 对象格式文本块
QTextFormat::CharFormat 2 对象格式单个字符
QTextFormat::ListFormat 3 对象格式列表
QTextFormat::FrameFormat 5 对象格式帧
QTextFormat::UserFormat 100

另请参阅 QTextCharFormat , QTextBlockFormat , QTextListFormat , QTextTableFormat ,和 type ().

enum QTextFormat:: ObjectTypes

This enum describes what kind of QTextObject this format is associated with.

常量 描述
QTextFormat::NoObject 0
QTextFormat::ImageObject 1
QTextFormat::TableObject 2
QTextFormat::TableCellObject 3
QTextFormat::UserObject 0x1000 The first object that can be used for application-specific purposes.

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

enum QTextFormat:: PageBreakFlag
flags QTextFormat:: PageBreakFlags

This enum describes how page breaking is performed when printing. It maps to the corresponding css properties.

常量 描述
QTextFormat::PageBreak_Auto 0 The page break is determined automatically depending on the available space on the current page
QTextFormat::PageBreak_AlwaysBefore 0x001 The page is always broken before the paragraph/table
QTextFormat::PageBreak_AlwaysAfter 0x010 A new page is always started after the paragraph/table

PageBreakFlags 类型是 typedef 对于 QFlags <PageBreakFlag>。它存储 PageBreakFlag 值的 OR 组合。

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

enum QTextFormat:: Property

此枚举描述不同特性可以拥有的格式。

常量 描述
QTextFormat::ObjectIndex 0x0 格式化对象的索引。见 objectIndex ().

段落和字符特性

常量 描述
QTextFormat::CssFloat 0x0800 How a frame is located relative to the surrounding text
QTextFormat::LayoutDirection 0x0801 The layout direction of the text in the document ( Qt::LayoutDirection ).
QTextFormat::OutlinePen 0x810
QTextFormat::ForegroundBrush 0x821
QTextFormat::BackgroundBrush 0x820
QTextFormat::BackgroundImageUrl 0x823

段落特性

常量 描述
QTextFormat::BlockAlignment 0x1010
QTextFormat::BlockTopMargin 0x1030
QTextFormat::BlockBottomMargin 0x1031
QTextFormat::BlockLeftMargin 0x1032
QTextFormat::BlockRightMargin 0x1033
QTextFormat::TextIndent 0x1034
QTextFormat::TabPositions 0x1035 Specifies the tab positions. The tab positions are structs of QTextOption::Tab which are stored in a QList (internally, in a QList < QVariant >).
QTextFormat::BlockIndent 0x1040
QTextFormat::LineHeight 0x1048
QTextFormat::LineHeightType 0x1049
QTextFormat::BlockNonBreakableLines 0x1050
QTextFormat::BlockTrailingHorizontalRulerWidth 0x1060 The width of a horizontal ruler element.
QTextFormat::HeadingLevel 0x1070 The level of a heading, for example 1 corresponds to an HTML H1 tag; otherwise 0. This enum value has been added in Qt 5.12.
QTextFormat::BlockCodeFence 0x1091 The character that was used in the "fences" around a Markdown code block. If the code block was indented rather than fenced, the block should not have this property. This enum value has been added in Qt 5.14.
QTextFormat::BlockQuoteLevel 0x1080 The depth of nested quoting on this block: 1 means the block is a top-level block quote. Blocks that are not block quotes should not have this property. This enum value has been added in Qt 5.14.
QTextFormat::BlockCodeLanguage 0x1090 The programming language in a preformatted or code block. Blocks that do not contain code should not have this property. This enum value has been added in Qt 5.14.
QTextFormat::BlockMarker 0x10A0 The type of adornment to be shown alongside the block. This enum value has been added in Qt 5.14.

字符特性

常量 描述
QTextFormat::FontFamily 0x2000 e{This property has been deprecated.} Use QTextFormat::FontFamilies instead.
QTextFormat::FontFamilies 0x1FE7
QTextFormat::FontStyleName 0x1FE8
QTextFormat::FontPointSize 0x2001
QTextFormat::FontPixelSize 0x2009
QTextFormat::FontSizeAdjustment 0x2002 Specifies the change in size given to the fontsize already set using FontPointSize or FontPixelSize.
QTextFormat::FontFixedPitch 0x2008
QTextFormat::FontWeight 0x2003
QTextFormat::FontItalic 0x2004
QTextFormat::FontUnderline 0x2005 此特性已弃用。 使用 QTextFormat::TextUnderlineStyle 代替。
QTextFormat::FontOverline 0x2006
QTextFormat::FontStrikeOut 0x2007
QTextFormat::FontCapitalization FirstFontProperty Specifies the capitalization type that is to be applied to the text.
QTextFormat::FontLetterSpacingType 0x1FE9 Specifies the meaning of the FontLetterSpacing property. The default is QFont::PercentageSpacing .
QTextFormat::FontLetterSpacing 0x1FE1 Changes the default spacing between individual letters in the font. The value is specified as a percentage or absolute value, depending on FontLetterSpacingType. The default value is 100%.
QTextFormat::FontWordSpacing 0x1FE2 Changes the default spacing between individual words. A positive value increases the word spacing by the corresponding pixels; a negative value decreases the spacing.
QTextFormat::FontStretch 0x1FEA 相当于 QFont::Stretch property
QTextFormat::FontStyleHint 0x1FE3 相当于 QFont::StyleHint property
QTextFormat::FontStyleStrategy 0x1FE4 相当于 QFont::StyleStrategy property
QTextFormat::FontKerning 0x1FE5 Specifies whether the font has kerning turned on.
QTextFormat::FontHintingPreference 0x1FE6 Controls the use of hinting according to values of the QFont::HintingPreference 枚举。
QTextFormat::TextUnderlineColor 0x2020 Specifies the color to draw underlines, overlines and strikeouts.
QTextFormat::TextVerticalAlignment 0x2021
QTextFormat::TextOutline 0x2022
QTextFormat::TextUnderlineStyle 0x2023
QTextFormat::TextToolTip 0x2024 Specifies the (optional) tool tip to be displayed for a fragment of text.
QTextFormat::TextSuperScriptBaseline 0x2025 Specifies the baseline (in % of height) of superscript texts.
QTextFormat::TextSubScriptBaseline 0x2026 Specifies the baseline (in % of height) of subscript texts.
QTextFormat::TextBaselineOffset 0x2027 Specifies the baseline (in % of height) of text. A positive value moves up the text, by the corresponding %; a negative value moves it down.
QTextFormat::IsAnchor 0x2030
QTextFormat::AnchorHref 0x2031
QTextFormat::AnchorName 0x2032
QTextFormat::ObjectType 0x2f00

列表特性

常量 描述
QTextFormat::ListStyle 0x3000 Specifies the style used for the items in a list, described by values of the QTextListFormat::Style 枚举。
QTextFormat::ListIndent 0x3001 Specifies the amount of indentation used for a list.
QTextFormat::ListNumberPrefix 0x3002 Defines the text which is prepended to item numbers in numeric lists.
QTextFormat::ListNumberSuffix 0x3003 Defines the text which is appended to item numbers in numeric lists.

Table and frame properties

常量 描述
QTextFormat::FrameBorder 0x4000
QTextFormat::FrameBorderBrush 0x4009
QTextFormat::FrameBorderStyle 0x4010 BorderStyle 枚举。
QTextFormat::FrameBottomMargin 0x4006
QTextFormat::FrameHeight 0x4004
QTextFormat::FrameLeftMargin 0x4007
QTextFormat::FrameMargin 0x4001
QTextFormat::FramePadding 0x4002
QTextFormat::FrameRightMargin 0x4008
QTextFormat::FrameTopMargin 0x4005
QTextFormat::FrameWidth 0x4003
QTextFormat::TableCellSpacing 0x4102
QTextFormat::TableCellPadding 0x4103
QTextFormat::TableColumns 0x4100
QTextFormat::TableColumnWidthConstraints 0x4101
QTextFormat::TableHeaderRowCount 0x4104
QTextFormat::TableBorderCollapse 0x4105 Specifies the QTextTableFormat::borderCollapse 特性。

Table cell properties

常量
QTextFormat::TableCellRowSpan 0x4810
QTextFormat::TableCellColumnSpan 0x4811
QTextFormat::TableCellLeftPadding 0x4814
QTextFormat::TableCellRightPadding 0x4815
QTextFormat::TableCellTopPadding 0x4812
QTextFormat::TableCellBottomPadding 0x4813

Table cell properties intended for use with QTextTableFormat::borderCollapse 被启用

常量
QTextFormat::TableCellTopBorder 0x4816
QTextFormat::TableCellBottomBorder 0x4817
QTextFormat::TableCellLeftBorder 0x4818
QTextFormat::TableCellRightBorder 0x4819
QTextFormat::TableCellTopBorderStyle 0x481a
QTextFormat::TableCellBottomBorderStyle 0x481b
QTextFormat::TableCellLeftBorderStyle 0x481c
QTextFormat::TableCellRightBorderStyle 0x481d
QTextFormat::TableCellTopBorderBrush 0x481e
QTextFormat::TableCellBottomBorderBrush 0x481f
QTextFormat::TableCellLeftBorderBrush 0x4820
QTextFormat::TableCellRightBorderBrush 0x4821

Image properties

常量 描述
QTextFormat::ImageName 0x5000 The filename or source of the image.
QTextFormat::ImageTitle 0x5001 The title attribute of an HTML image tag, or the quoted string that comes after the URL in a Markdown image link. This enum value has been added in Qt 5.14.
QTextFormat::ImageAltText 0x5002 The alt attribute of an HTML image tag, or the image description in a Markdown image link. This enum value has been added in Qt 5.14.
QTextFormat::ImageWidth 0x5010
QTextFormat::ImageHeight 0x5011
QTextFormat::ImageQuality 0x5014

Selection properties

常量 描述
QTextFormat::FullWidthSelection 0x06000 When set on the characterFormat of a selection, the whole width of the text will be shown selected.

Page break properties

常量 描述
QTextFormat::PageBreakPolicy 0x7000 Specifies how pages are broken. See the PageBreakFlag 枚举。
QTextFormat::UserProperty 0x100000

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

成员函数文档编制

QTextFormat:: QTextFormat ()

Creates a new text format with an InvalidFormat .

另请参阅 FormatType .

[explicit] QTextFormat:: QTextFormat ( int type )

Creates a new text format of the given type .

另请参阅 FormatType .

QTextFormat:: QTextFormat (const QTextFormat & other )

Creates a new text format with the same attributes as the other text format.

QTextFormat:: ~QTextFormat ()

Destroys this text format.

QBrush QTextFormat:: background () const

Returns the brush used to paint the document's background.

另请参阅 setBackground (), clearBackground (),和 foreground ().

bool QTextFormat:: boolProperty ( int propertyId ) const

Returns the value of the property specified by propertyId . If the property isn't of QTextFormat::Bool type, false is returned instead.

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

QBrush QTextFormat:: brushProperty ( int propertyId ) const

Returns the value of the property given by propertyId ; if the property isn't of QMetaType::QBrush type, Qt::NoBrush 被返回取而代之。

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

void QTextFormat:: clearBackground ()

Clears the brush used to paint the document's background. The default brush will be used.

另请参阅 background (), setBackground (),和 clearForeground ().

void QTextFormat:: clearForeground ()

Clears the brush used to paint the document's foreground. The default brush will be used.

另请参阅 foreground (), setForeground (),和 clearBackground ().

void QTextFormat:: clearProperty ( int propertyId )

Clears the value of the property given by propertyId

另请参阅 Property .

QColor QTextFormat:: colorProperty ( int propertyId ) const

Returns the value of the property given by propertyId ; if the property isn't of QMetaType::QColor type, an invalid color is returned instead.

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

qreal QTextFormat:: doubleProperty ( int propertyId ) const

Returns the value of the property specified by propertyId . If the property isn't of QMetaType::Double or QMetaType::Float type, 0 is returned instead.

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

QBrush QTextFormat:: foreground () const

Returns the brush used to render foreground details, such as text, frame outlines, and table borders.

另请参阅 setForeground (), clearForeground (),和 background ().

bool QTextFormat:: hasProperty ( int propertyId ) const

返回 true if the text format has a property with the given propertyId ;否则返回 false .

另请参阅 properties () 和 Property .

int QTextFormat:: intProperty ( int propertyId ) const

Returns the value of the property specified by propertyId . If the property is not of QTextFormat::Integer type, 0 is returned instead.

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

bool QTextFormat:: isBlockFormat () const

返回 true if this text format is a BlockFormat ;否则返回 false .

bool QTextFormat:: isCharFormat () const

返回 true if this text format is a CharFormat ;否则返回 false .

bool QTextFormat:: isEmpty () const

Returns true if the format does not store any properties; false otherwise.

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

bool QTextFormat:: isFrameFormat () const

返回 true if this text format is a FrameFormat ;否则返回 false .

bool QTextFormat:: isImageFormat () const

返回 true if this text format is an image format; otherwise returns false .

bool QTextFormat:: isListFormat () const

返回 true if this text format is a ListFormat ;否则返回 false .

bool QTextFormat:: isTableCellFormat () const

返回 true if this text format is a TableCellFormat ;否则返回 false .

bool QTextFormat:: isTableFormat () const

返回 true if this text format is a TableFormat ;否则返回 false .

bool QTextFormat:: isValid () const

返回 true if the format is valid (i.e. is not InvalidFormat );否则返回 false .

Qt::LayoutDirection QTextFormat:: layoutDirection () const

Returns the document's layout direction.

另请参阅 setLayoutDirection ().

QTextLength QTextFormat:: lengthProperty ( int propertyId ) const

Returns the value of the property given by propertyId .

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

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

Returns the value of the property given by propertyId . If the property isn't of QTextFormat::LengthVector type, an empty list is returned instead.

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

void QTextFormat:: merge (const QTextFormat & other )

Merges the other format with this format; where there are conflicts the other format takes precedence.

int QTextFormat:: objectIndex () const

Returns the index of the format object, or -1 if the format object is invalid.

另请参阅 setObjectIndex ().

int QTextFormat:: objectType () const

Returns the text format's object type.

另请参阅 ObjectTypes and setObjectType ().

QPen QTextFormat:: penProperty ( int propertyId ) const

Returns the value of the property given by propertyId ; if the property isn't of QMetaType::QPen type, Qt::NoPen 被返回取而代之。

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

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

Returns a map with all properties of this text format.

QVariant QTextFormat:: property ( int propertyId ) const

Returns the property specified by the given propertyId .

另请参阅 setProperty () 和 Property .

int QTextFormat:: propertyCount () const

Returns the number of properties stored in the format.

void QTextFormat:: setBackground (const QBrush & brush )

Sets the brush use to paint the document's background to the brush 指定。

另请参阅 background (), clearBackground (),和 setForeground ().

void QTextFormat:: setForeground (const QBrush & brush )

Sets the foreground brush to the specified brush . The foreground brush is mostly used to render text.

另请参阅 foreground (), clearForeground (),和 setBackground ().

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

Sets the document's layout direction to the specified direction .

另请参阅 layoutDirection ().

void QTextFormat:: setObjectIndex ( int index )

Sets the format object's object index .

另请参阅 objectIndex ().

void QTextFormat:: setObjectType ( int type )

Sets the text format's object type to type .

另请参阅 ObjectTypes and objectType ().

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

Sets the property specified by the propertyId 到给定 value .

另请参阅 property () 和 Property .

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

Sets the value of the property given by propertyId to value .

另请参阅 lengthVectorProperty () 和 Property .

QString QTextFormat:: stringProperty ( int propertyId ) const

Returns the value of the property given by propertyId ; if the property isn't of QMetaType::QString type, an empty string is returned instead.

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

void QTextFormat:: swap ( QTextFormat & other )

Swaps this text format with other 。此函数非常快且从不失败。

QTextBlockFormat QTextFormat:: toBlockFormat () const

Returns this format as a block format.

QTextCharFormat QTextFormat:: toCharFormat () const

Returns this format as a character format.

QTextFrameFormat QTextFormat:: toFrameFormat () const

Returns this format as a frame format.

QTextImageFormat QTextFormat:: toImageFormat () const

Returns this format as an image format.

QTextListFormat QTextFormat:: toListFormat () const

Returns this format as a list format.

QTextTableCellFormat QTextFormat:: toTableCellFormat () const

Returns this format as a table cell format.

QTextTableFormat QTextFormat:: toTableFormat () const

Returns this format as a table format.

int QTextFormat:: type () const

返回此格式的类型。

另请参阅 FormatType .

QVariant QTextFormat:: operator QVariant () const

Returns the text format as a QVariant

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

返回 true if this text format is different from the other text format.

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

赋值 other text format to this text format, and returns a reference to this text format.

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

返回 true if this text format is the same as the other text format.