QTextBlockFormat Class
QTextBlockFormat 类为QTextDocument 中的文本块提供格式设置信息。更多内容...
| 标题: | #include <QTextBlockFormat> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Gui) target_link_libraries(mytarget PRIVATE Qt6::Gui) |
| qmake: | QT += gui |
| 继承自: | QTextFormat |
- 所有成员列表(包括继承的成员)
- QTextBlockFormat 属于“富文本处理 API”和“隐式共享类”的一部分。
注意:该类中的所有函数均为可重入函数。
公共类型
| enum | LineHeightTypes { SingleHeight, ProportionalHeight, FixedHeight, MinimumHeight, LineDistanceHeight } |
| enum class | MarkerType { NoMarker, Unchecked, Checked } |
公共函数
| QTextBlockFormat() | |
| Qt::Alignment | alignment() const |
| qreal | bottomMargin() const |
| int | headingLevel() const |
| int | indent() const |
| bool | isValid() const |
| qreal | leftMargin() const |
| qreal | lineHeight() const |
| qreal | lineHeight(qreal scriptLineHeight, qreal scaling = 1.0) const |
| int | lineHeightType() const |
| QTextBlockFormat::MarkerType | marker() const |
| bool | nonBreakableLines() const |
| QTextFormat::PageBreakFlags | pageBreakPolicy() const |
| qreal | rightMargin() const |
| void | setAlignment(Qt::Alignment alignment) |
| void | setBottomMargin(qreal margin) |
| void | setHeadingLevel(int level) |
| void | setIndent(int indentation) |
| void | setLeftMargin(qreal margin) |
| void | setLineHeight(qreal height, int heightType) |
| void | setMarker(QTextBlockFormat::MarkerType marker) |
| void | setNonBreakableLines(bool b) |
| void | setPageBreakPolicy(QTextFormat::PageBreakFlags policy) |
| void | setRightMargin(qreal margin) |
| void | setTabPositions(const QList<QTextOption::Tab> &tabs) |
| void | setTextIndent(qreal indent) |
| void | setTopMargin(qreal margin) |
| QList<QTextOption::Tab> | tabPositions() const |
| qreal | textIndent() const |
| qreal | topMargin() const |
详细说明
文档由一组块组成,这些块由QTextBlock 对象表示。每个块可以包含某种类型的项目,例如一段文本、一个表格、一个列表或一张图片。每个块都有一个相关的 QTextBlockFormat,用于指定其特性。
为了适应从左到右和从右到左的语言,您可以使用setLayoutDirection()设置块的方向。段落对齐方式通过setAlignment()设置。边距由setTopMargin()、setBottomMargin()、setLeftMargin()和setRightMargin()控制。整体缩进通过setIndent()设置,首行缩进通过setTextIndent()设置。
行间距通过setLineHeight() 进行设置,并通过lineHeight() 和lineHeightType() 获取。可用的行间距类型在LineHeightTypes 枚举中。
可通过setNonBreakableLines() 启用或禁用换行。
用于绘制段落背景的画笔通过setBackground() 设置,文本外观的其他方面可通过setProperty() 函数并配合OutlinePen 、ForegroundBrush 和BackgroundBrush QTextFormat::Property 值进行自定义。
如果文本块是列表的一部分,它还可以采用列表格式,可通过 listFormat() 函数访问该格式。
另请参阅 QTextBlock 和QTextCharFormat 。
成员类型文档
enum QTextBlockFormat::LineHeightTypes
此枚举描述了段落可支持的各种行间距类型。
| 常量 | 值 | 描述 |
|---|---|---|
QTextBlockFormat::SingleHeight | 0 | 这是默认行高:单倍行距。 |
QTextBlockFormat::ProportionalHeight | 1 | 这将间距设置为与行高成比例(以百分比表示)。例如,设置为 200 即为双倍行距。 |
QTextBlockFormat::FixedHeight | 2 | 此参数将行高设置为固定的行高(以像素为单位)。 |
QTextBlockFormat::MinimumHeight | 3 | 此参数用于设置最小行高(以像素为单位)。 |
QTextBlockFormat::LineDistanceHeight | 4 | 这会在行与行之间添加指定的高度(以像素为单位)。 |
另请参阅 lineHeight()、lineHeightType(),以及setLineHeight()。
enum class QTextBlockFormat::MarkerType
此枚举描述了列表项可能具有的标记类型。如果某个列表项(即调用 `QTextBlock::textList()` 返回该列表的段落)具有标记,则会渲染该标记,而非常规的项目符号。 通过这种方式,可在同一列表中混合使用可选中列表项和普通列表项,从而覆盖QTextListFormat::style() 函数为整个列表指定的项目符号类型。
| 常量 | 值 | 描述 |
|---|---|---|
QTextBlockFormat::MarkerType::NoMarker | 0 | 这是默认设置:将显示列表项的项目符号。 |
QTextBlockFormat::MarkerType::Unchecked | 1 | 将显示一个未选中的复选框,而不是列表项的项目符号。 |
QTextBlockFormat::MarkerType::Checked | 2 | 将显示一个已选中的复选框,而不是列表项的项目符号。 |
将来,此功能可能会扩展,以指定其他类型的段落装饰。
另请参阅 QTextListFormat::style()。
成员函数文档
QTextBlockFormat::QTextBlockFormat()
创建一个新的 QTextBlockFormat。
Qt::Alignment QTextBlockFormat::alignment() const
返回段落的对齐方式。
另请参阅 setAlignment()。
qreal QTextBlockFormat::bottomMargin() const
返回段落的底部边距。
另请参阅 setBottomMargin()和topMargin()。
int QTextBlockFormat::headingLevel() const
如果该段落是标题,则返回其标题级别;否则返回 0。
另请参阅 setHeadingLevel()。
int QTextBlockFormat::indent() const
返回该段落的缩进量。
另请参阅 setIndent()。
bool QTextBlockFormat::isValid() const
如果该块格式有效,则返回true ;否则返回false。
qreal QTextBlockFormat::leftMargin() const
返回该段落的左边距。
另请参阅 setLeftMargin()、rightMargin() 以及indent()。
qreal QTextBlockFormat::lineHeight() const
该方法返回该段落的 LineHeight 属性。
另请参阅 LineHeightTypes 、setLineHeight() 以及lineHeightType()。
qreal QTextBlockFormat::lineHeight(qreal scriptLineHeight, qreal scaling = 1.0) const
根据scriptLineHeight 指定的脚本行高度和指定的scaling 因子,返回段落中各行的高度。
返回的值还取决于段落的 LineHeightType 属性以及为该段落设置的 LineHeight 参数。
对于包含固定像素数的高程,需要进行缩放,以便在打印时进行适当调整。
另请参阅 LineHeightTypes 、setLineHeight() 和lineHeightType()。
int QTextBlockFormat::lineHeightType() const
此方法返回段落的 LineHeightType 属性。
另请参阅 LineHeightTypes 、setLineHeight() 以及lineHeight()。
QTextBlockFormat::MarkerType QTextBlockFormat::marker() const
如果已设置段落标记,则返回该标记;否则返回NoMarker 。
另请参阅 setMarker()。
bool QTextBlockFormat::nonBreakableLines() const
如果段落中的行不可换行,则返回 `true `;否则返回 `false`。
另请参阅 setNonBreakableLines()。
QTextFormat::PageBreakFlags QTextBlockFormat::pageBreakPolicy() const
返回该段落当前设置的分页策略。默认值为QTextFormat::PageBreak_Auto 。
另请参阅 setPageBreakPolicy()。
qreal QTextBlockFormat::rightMargin() const
返回段落的右边距。
另请参阅 setRightMargin() 和leftMargin()。
void QTextBlockFormat::setAlignment(Qt::Alignment alignment)
设置段落的alignment 。
另请参阅 alignment()。
void QTextBlockFormat::setBottomMargin(qreal margin)
设置段落的底部margin 。
另请参阅 bottomMargin()、setTopMargin()、setLeftMargin() 和setRightMargin()。
void QTextBlockFormat::setHeadingLevel(int level)
设置段落的标题级别level ,其中 1 代表最高级别的标题类型(通常采用最大的标题字号),数值越大,在文档中的层级越深(通常字号也越小)。 例如,在读取 HTML H1 标签时,标题级别会被设置为 1。设置标题级别并不会自动更改字体大小;但QTextDocumentFragment::fromHtml() 会同时设置标题级别和字体大小。
如果该段落不是标题,则应将级别设置为 0(默认值)。
另请参阅 headingLevel()。
void QTextBlockFormat::setIndent(int indentation)
设置段落的“indentation ”。边距可通过setLeftMargin()和setTextIndent()独立于缩进进行设置。indentation 是一个整数,它与文档范围内的标准缩进值相乘,从而得出段落的实际缩进量。
另请参阅 indent() 和QTextDocument::indentWidth()。
void QTextBlockFormat::setLeftMargin(qreal margin)
设置段落的左侧缩进margin 。缩进可通过setIndent()单独设置。
另请参阅 leftMargin()、setRightMargin()、setTopMargin() 以及setBottomMargin()。
void QTextBlockFormat::setLineHeight(qreal height, int heightType)
将段落的行高设置为height 指定的值,该值取决于heightType ,具体方式由LineHeightTypes 枚举所描述。
另请参阅 LineHeightTypes 、lineHeight() 以及lineHeightType()。
void QTextBlockFormat::setMarker(QTextBlockFormat::MarkerType marker)
将应与段落一同渲染的装饰类型设置为marker 。例如,列表项可以使用复选框(无论是否被选中)作为其项目符号的替代装饰。默认值为NoMarker 。
另请参阅 marker()。
void QTextBlockFormat::setNonBreakableLines(bool b)
如果b 为真,则段落中的行被视为不可换行;否则,这些行可换行。
另请参阅 nonBreakableLines()。
void QTextBlockFormat::setPageBreakPolicy(QTextFormat::PageBreakFlags policy)
将该段落的分页策略设置为policy 。
另请参阅 pageBreakPolicy()。
void QTextBlockFormat::setRightMargin(qreal margin)
设置段落的右对齐方式margin 。
另请参阅 rightMargin()、setLeftMargin()、setTopMargin() 以及setBottomMargin()。
void QTextBlockFormat::setTabPositions(const QList<QTextOption::Tab> &tabs)
将文本块的制表符位置设置为tabs 中指定的位置。
另请参阅 tabPositions()。
void QTextBlockFormat::setTextIndent(qreal indent)
设置块中第一行的indent 。这使得段落的第一行可以与其他行采用不同的缩进方式,从而提高文本的可读性。
另请参阅 textIndent()、setLeftMargin()、setRightMargin()、setTopMargin() 以及setBottomMargin()。
void QTextBlockFormat::setTopMargin(qreal margin)
设置段落的顶部margin 。
另请参阅 topMargin()、setBottomMargin()、setLeftMargin() 以及setRightMargin()。
QList<QTextOption::Tab> QTextBlockFormat::tabPositions() const
返回为该文本块定义的制表符位置列表。
另请参阅 setTabPositions()。
qreal QTextBlockFormat::textIndent() const
返回段落的文本缩进。
另请参阅 ` setTextIndent()`。
qreal QTextBlockFormat::topMargin() const
返回该段落的上边距。
另请参阅 setTopMargin() 和bottomMargin()。
© 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.