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

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