本页内容

QTextFragment Class

QTextFragment 类在QTextDocument 中保存一段文本,该文本由单个QTextCharFormat 组成。更多内容...

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

注意:该类中的所有函数均为可重入函数。

公共函数

QTextFragment()
QTextFragment(const QTextFragment &other)
QTextCharFormat charFormat() const
int charFormatIndex() const
bool contains(int position) const
QList<QGlyphRun> glyphRuns(int pos = -1, int len = -1) const
bool isValid() const
int length() const
int position() const
QString text() const
bool operator!=(const QTextFragment &other) const
bool operator<(const QTextFragment &other) const
QTextFragment &operator=(const QTextFragment &other)
bool operator==(const QTextFragment &other) const

详细说明

文本片段描述了一段以单一字符格式存储的文本。字符格式发生变化的文本可以通过具有不同格式的文本片段序列来表示。

如果用户编辑片段中的文本并引入了不同的字符格式,则该片段的文本将在每个格式发生变化的点处被拆分,并创建新的片段。 例如,更改句子中间某些文本的样式会导致该片段被分成三个独立的片段:第一个和第三个与之前格式相同,第二个采用新样式。 第一个片段将包含句首的文本,第二个片段将包含句中的文本,第三个片段将包含句末的文本。

截图展示了当中间的文字加粗时,文本如何被分成三个部分。

可以通过text()和charFormat()函数获取片段的文本和字符格式。length()函数返回片段中文本的长度。position()返回片段起始位置在文档中的位置。要确定片段是否包含文档中的特定位置,请使用contains()函数。

另请参阅《 QTextDocument 》 和《富文本文档结构》。

成员函数文档

QTextFragment::QTextFragment()

创建一个新的空文本片段。

QTextFragment::QTextFragment(const QTextFragment &other)

将other 文本片段的内容(文本和格式)复制到此文本片段中。

QTextCharFormat QTextFragment::charFormat() const

返回文本片段的字符格式。

另请参阅 ` text()`。

int QTextFragment::charFormatIndex() const

返回该文本片段的字符格式在文档内部字符格式列表中的索引。

另请参阅 QTextDocument::allFormats()。

bool QTextFragment::contains(int position) const

如果文本片段包含文档中指定位置(position )处的文本,则返回true ;否则返回false 。

QList<QGlyphRun> QTextFragment::glyphRuns(int pos = -1, int len = -1) const

返回该文本片段中从位置pos 开始的、对应于len 个字符的字形。字形的位置相对于QTextBlock 的布局位置。

如果pos 小于零,则默认从QTextFragment 的开头开始。如果len 小于零,则默认取该片段的长度。

另请参阅 QGlyphRun 、QTextBlock::layout()、QTextLayout::position() 以及QPainter::drawGlyphRun()。

bool QTextFragment::isValid() const

如果这是一个有效的文本片段(即在文档中具有有效位置),则返回true ;否则返回false 。

int QTextFragment::length() const

返回文本片段中的字符数。

另请参阅 ` text()`。

int QTextFragment::position() const

返回该文本片段在文档中的位置。

QString QTextFragment::text() const

将文本片段作为纯文本返回。

另请参阅 length() 和charFormat()。

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

如果该文本片段与other 文本片段不同(位于不同位置),则返回true ;否则返回false。

bool QTextFragment::operator<(const QTextFragment &other) const

如果该文本片段在文档中的出现位置早于other 文本片段,则返回true ;否则返回false 。

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

将other 文本片段的内容(文本和格式)分配给此文本片段。

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

如果该文本片段与other 中的文本片段(在相同位置)相同,则返回true ;否则返回false 。

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