本页内容

QTextBoundaryFinder Class

QTextBoundaryFinder 类提供了一种在字符串中查找 Unicode 文本边界的方法。更多内容...

头文件: #include <QTextBoundaryFinder>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

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

公共类型

enum BoundaryReason { NotAtBoundary, BreakOpportunity, StartOfItem, EndOfItem, MandatoryBreak, SoftHyphen }
flags BoundaryReasons
enum BoundaryType { Grapheme, Word, Line, Sentence }

公共函数

QTextBoundaryFinder()
QTextBoundaryFinder(QTextBoundaryFinder::BoundaryType type, const QString &string)
(since 6.0) QTextBoundaryFinder(QTextBoundaryFinder::BoundaryType type, QStringView string, unsigned char *buffer = nullptr, qsizetype bufferSize = 0)
QTextBoundaryFinder(QTextBoundaryFinder::BoundaryType type, const QChar *chars, qsizetype length, unsigned char *buffer = nullptr, qsizetype bufferSize = 0)
QTextBoundaryFinder(const QTextBoundaryFinder &other)
(since 6.11) QTextBoundaryFinder(QTextBoundaryFinder &&other)
~QTextBoundaryFinder()
QTextBoundaryFinder::BoundaryReasons boundaryReasons() const
bool isAtBoundary() const
bool isValid() const
qsizetype position() const
void setPosition(qsizetype position)
QString string() const
(since 6.11) void swap(QTextBoundaryFinder &other)
void toEnd()
qsizetype toNextBoundary()
qsizetype toPreviousBoundary()
void toStart()
QTextBoundaryFinder::BoundaryType type() const
(since 6.11) QTextBoundaryFinder &operator=(QTextBoundaryFinder &&other)
QTextBoundaryFinder &operator=(const QTextBoundaryFinder &other)

详细说明

QTextBoundaryFinder 允许根据 Unicode 文本边界规范(参见《Unicode 标准附录 #14》和《Unicode 标准附录 #29》)在字符串中查找 Unicode 文本边界。

QTextBoundaryFinder 可以根据 `BoundaryType` 的值,以四种可能的模式对 `QString ` 进行操作。

构成用户所认为的字符或语言基本单位的 Unicode 字符单元在此称为“字素簇”。 例如,两个 Unicode 字符“A”+分音符构成一个字素簇,因为用户将它们视为一个字符,但在这种情况下,它们却由两个 Unicode 码点表示(参见https://www.unicode.org/reports/tr29/#Grapheme_Cluster_Boundaries)。

词界用于定位一种语言中被视为单词的起始和结束位置(参见https://www.unicode.org/reports/tr29/#Word_Boundaries)。

换行边界给出了可能发生换行的地方,而句子边界则显示整个句子的起始和结束位置(参见https://www.unicode.org/reports/tr29/#Sentence_Boundaries和https://www.unicode.org/reports/tr14/)。

字符串中的第一个位置始终是一个有效的边界,它指的是第一个字符之前的那个位置。字符串长度所对应的最后一个位置也是有效的,它指的是最后一个字符之后的那个位置。

成员类型文档

enum QTextBoundaryFinder::BoundaryReason
flags QTextBoundaryFinder::BoundaryReasons

常数值描述
QTextBoundaryFinder::NotAtBoundary0边界查找器未位于边界位置。
QTextBoundaryFinder::BreakOpportunity0x1f分界点定位器位于断行机会位置。此类断行机会也可能是项目边界(即 StartOfItem、EndOfItem 或两者的组合)、强制换行或软连字符。
QTextBoundaryFinder::StartOfItem (since Qt 5.0)0x20分界点定位器位于一个音素、一个单词、一个句子或一行文字的开头。
QTextBoundaryFinder::EndOfItem (since Qt 5.0)0x40边界查找器位于一个字形、单词、句子或行末尾。
QTextBoundaryFinder::MandatoryBreak (since Qt 5.0)0x80边界查找器位于行尾(仅适用于“行”边界类型)。
QTextBoundaryFinder::SoftHyphen0x100边界查找器位于软连字符处(仅适用于“行”边界类型)。

BoundaryReasons 类型是QFlags<BoundaryReason> 的 typedef 定义。它存储 BoundaryReason 值的“或”组合。

enum QTextBoundaryFinder::BoundaryType

常数值描述
QTextBoundaryFinder::Grapheme0查找作为最小边界的字符。它包括字母、标点符号、数字等。
QTextBoundaryFinder::Word1查找单词。
QTextBoundaryFinder::Line3查找将文本拆分为多行时的可能位置。
QTextBoundaryFinder::Sentence2查找句子分界点。这些包括句号、问号等。

成员函数文档

QTextBoundaryFinder::QTextBoundaryFinder()

构建了一个无效的 QTextBoundaryFinder 对象。

QTextBoundaryFinder::QTextBoundaryFinder(QTextBoundaryFinder::BoundaryType type, const QString &string)

创建一个type 类型的QTextBoundaryFinder对象,该对象作用于string 。

[since 6.0] QTextBoundaryFinder::QTextBoundaryFinder(QTextBoundaryFinder::BoundaryType type, QStringView string, unsigned char *buffer = nullptr, qsizetype bufferSize = 0)

创建一个type 大小的QTextBoundaryFinder对象,用于处理string 。

buffer 是一个可选的工作缓冲区,其大小为bufferSize ,您可以将其传递给QTextBoundaryFinder。如果该缓冲区足够大,能够容纳所需的工作数据(bufferSize >= length + 1),则它将使用此缓冲区,而非自行分配缓冲区。

警告:QTextBoundaryFinder 不会创建string 的副本。应用程序开发人员有责任确保该数组在 QTextBoundaryFinder 对象存活期间始终被分配内存。此规则同样适用于buffer 。

该函数自 Qt 6.0 起引入。

QTextBoundaryFinder::QTextBoundaryFinder(QTextBoundaryFinder::BoundaryType type, const QChar *chars, qsizetype length, unsigned char *buffer = nullptr, qsizetype bufferSize = 0)

与 QTextBoundaryFinder(type,QStringView(chars, length), buffer, bufferSize) 相同。

这是一个重载函数。

QTextBoundaryFinder::QTextBoundaryFinder(const QTextBoundaryFinder &other)

复制 QTextBoundaryFinder 对象,即other 。

[noexcept, since 6.11] QTextBoundaryFinder::QTextBoundaryFinder(QTextBoundaryFinder &&other)

从other 创建一个新的 QTextBoundaryFinder 对象。

注意: 被移动的源对象 other 将处于部分初始化状态,在此状态下,唯一有效的操作是销毁和赋予新值。

该函数于 Qt 6.11 中引入。

[noexcept] QTextBoundaryFinder::~QTextBoundaryFinder()

销毁QTextBoundaryFinder 对象。

QTextBoundaryFinder::BoundaryReasons QTextBoundaryFinder::boundaryReasons() const

返回边界查找器将当前位置选为边界的原因。

bool QTextBoundaryFinder::isAtBoundary() const

如果对象的position()当前位于一个有效的文本边界处,则返回true 。

bool QTextBoundaryFinder::isValid() const

如果文本边界查找器有效,则返回true ;否则返回false 。默认的QTextBoundaryFinder 是无效的。

qsizetype QTextBoundaryFinder::position() const

返回QTextBoundaryFinder 的当前位置。

范围从 0(字符串开头)到字符串长度(包含该值)。

另请参阅 setPosition()。

void QTextBoundaryFinder::setPosition(qsizetype position)

将QTextBoundaryFinder 的当前位置设置为position 。

如果 `position ` 超出范围,它将被限制在仅有效的位置范围内。在这种情况下,有效位置范围为 0 到字符串长度(包含 0 和字符串长度)。

另请参阅 position()。

QString QTextBoundaryFinder::string() const

返回QTextBoundaryFinder 对象所操作的字符串。

[noexcept, since 6.11] void QTextBoundaryFinder::swap(QTextBoundaryFinder &other)

将此文本边界查找器替换为other 。该操作速度极快,且从不会失败。

该函数于 Qt 6.11 中引入。

void QTextBoundaryFinder::toEnd()

将光标移至字符串末尾。这相当于setPosition(string.length())。

另请参阅 setPosition() 和position()。

qsizetype QTextBoundaryFinder::toNextBoundary()

将QTextBoundaryFinder 移动到下一个边界位置,并返回该位置。

如果不存在下一个边界,则返回 -1。

qsizetype QTextBoundaryFinder::toPreviousBoundary()

将QTextBoundaryFinder 移动到上一个边界位置,并返回该位置。

如果不存在前一个边界,则返回 -1。

void QTextBoundaryFinder::toStart()

将光标移至字符串开头。这相当于setPosition(0)。

另请参阅 setPosition() 和position()。

QTextBoundaryFinder::BoundaryType QTextBoundaryFinder::type() const

返回QTextBoundaryFinder 的类型。

[noexcept, since 6.11] QTextBoundaryFinder &QTextBoundaryFinder::operator=(QTextBoundaryFinder &&other)

将other 通过移动赋值操作赋值给此QTextBoundaryFinder 实例。

注意: 被移动的对象 other 将处于部分初始化的状态,在此状态下,唯一有效的操作是销毁和赋值。

该函数在 Qt 6.11 中引入。

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

将对象other 赋值给另一个QTextBoundaryFinder 对象。

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