QCollator Class
QCollator 类根据本地化的整理算法比较字符串。更多
头文件: | #include <QCollator> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
qmake: | QT += core |
- 所有成员(包括继承成员)的列表
- QCollator 属于字符串数据类和隐式共享类。
注意:该类中的所有函数都是可重入的。
公共函数
QCollator() | |
QCollator(const QLocale &locale) | |
QCollator(const QCollator &other) | |
QCollator(QCollator &&other) | |
~QCollator() | |
Qt::CaseSensitivity | caseSensitivity() const |
int | compare(QStringView s1, QStringView s2) const |
int | compare(const QString &s1, const QString &s2) const |
int | compare(const QChar *s1, qsizetype len1, const QChar *s2, qsizetype len2) const |
bool | ignorePunctuation() const |
QLocale | locale() const |
bool | numericMode() const |
void | setCaseSensitivity(Qt::CaseSensitivity cs) |
void | setIgnorePunctuation(bool on) |
void | setLocale(const QLocale &locale) |
void | setNumericMode(bool on) |
QCollatorSortKey | sortKey(const QString &string) const |
void | swap(QCollator &other) |
bool | operator()(QStringView s1, QStringView s2) const |
bool | operator()(const QString &s1, const QString &s2) const |
QCollator & | operator=(QCollator &&other) |
QCollator & | operator=(const QCollator &other) |
静态公共成员
(since 6.3) int | defaultCompare(QStringView s1, QStringView s2) |
(since 6.3) QCollatorSortKey | defaultSortKey(QStringView key) |
详细说明
QCollator 使用QLocale 进行初始化。然后,它就可以使用适合该地域的排序方式对字符串进行比较和排序。
QCollator 对象可与基于模板的排序算法(如 std::sort())一起使用,对包含QString 条目的列表进行排序。
QStringList sortedStrings(QStringList seq) { QCollator order; std::sort(seq.begin(), seq.end(), order); return seq; }
除本地语言外,还可以设置几个可选标志来影响排序结果。
POSIX 回退实现
在 Unix 系统上,Qt 通常编译为使用 ICU(macOS 除外,Qt 默认使用等效的 Apple API)。不过,如果编译时 ICU 不可用或被显式禁用,Qt 将使用仅使用 POSIX API 的后端。这个后端有几个限制:
- 仅支持QLocale::c() 和QLocale::system() 本地化。有关系统语言的更多信息,请查阅
<locale.h>
头文件的 POSIX 和 C 标准库手册。 - caseSensitivity(不支持():只能执行区分大小写的校对。
- numericMode不支持 () 和ignorePunctuation()。
使用任何不支持的选项都会在应用程序输出中打印警告。
成员函数文档
QCollator::QCollator()
使用默认语言的校对语言构造 QCollator。
作为默认本地语言使用的系统本地语言,可能有一个与自身不同的校对本地语言(例如,在 Unix 环境中,如果 LC_COLLATE 与 LANG 设置不同)。所有其他本地语言都是自己的校对本地语言。
另请参阅 setLocale()、QLocale::collation() 和QLocale::setDefault()。
[explicit]
QCollator::QCollator(const QLocale &locale)
使用给定的locale 构建 QCollator。
另请参见 setLocale().
QCollator::QCollator(const QCollator &other)
创建other 的副本。
[noexcept]
QCollator::QCollator(QCollator &&other)
移动构造函数。将other 移入此整理器。
注意: 从other 移入的对象处于部分形成状态,在这种状态下,唯一有效的操作是销毁和赋新值。
[noexcept]
QCollator::~QCollator()
销毁此拼版器。
Qt::CaseSensitivity QCollator::caseSensitivity() const
返回校对器的大小写敏感性。
在设置之前,默认为大小写敏感。
注意: 在 C 语言区,大小写敏感时,所有小写字母排序在所有大写字母之后,而大多数语言区则将每个小写字母排序在其大写字母伙伴之前或之后。因此,"Zap "在 C 语言区排序在 "ape "之前,而在大多数其他语言区则排序在 "ape "之后。
另请参见 setCaseSensitivity()。
int QCollator::compare(QStringView s1, QStringView s2) const
将s1 与s2 进行比较。
如果s1 小于s2 ,则返回一个负整数;如果大于s2 ,则返回一个正整数;如果两者相等,则返回 0。
int QCollator::compare(const QString &s1, const QString &s2) const
这是一个重载函数。
int QCollator::compare(const QChar *s1, qsizetype len1, const QChar *s2, qsizetype len2) const
这是一个重载函数。
将s1 与s2 进行比较。len1 和len2 指定s1 和s2 指向的QChar 数组的长度。
如果s1 小于s2 ,则返回一个负整数;如果大于s2 ,则返回一个正整数;如果两者相等,则返回 0。
注意: 在 6.4 之前的 Qt XML 版本中,长度参数的类型是int
,而不是qsizetype
。
[static, since 6.3]
int QCollator::defaultCompare(QStringView s1, QStringView s2)
比较字符串s1 和s2 ,返回它们的排序顺序。该函数对默认构造的QCollator 对象执行与compare() 相同的操作。
此函数在 Qt 6.3 中引入。
另请参阅 compare() 和defaultSortKey()。
[static, since 6.3]
QCollatorSortKey QCollator::defaultSortKey(QStringView key)
返回字符串key 的排序键。此函数在默认构造的QCollator 对象上执行与sortKey() 相同的操作。
此函数在 Qt 6.3 中引入。
另请参阅 sortKey() 和defaultCompare()。
bool QCollator::ignorePunctuation() const
返回整理时是否忽略标点符号。
当true
时,比较字符串时会忽略所有标点符号。
另请参阅 setIgnorePunctuation()。
QLocale QCollator::locale() const
返回校对器的区域设置。
除非向构造函数或通过调用setLocale() 提供,否则将使用系统默认的校对域。
另请参阅 setLocale() 和QLocale::collation()。
bool QCollator::numericMode() const
如果启用了数字排序,则返回true
,否则返回false
。
当true
时,数字被识别为数字,并按算术顺序排序;例如,100 排序在 99 之后。false
时,数字按词法顺序排序,因此 100 排在 99 之前(因为 1 在 9 之前)。默认情况下,该选项被禁用。
另请参阅 setNumericMode() 。
void QCollator::setCaseSensitivity(Qt::CaseSensitivity cs)
将校对工具的大小写敏感性设置为cs 。
另请参阅 caseSensitivity() 。
void QCollator::setIgnorePunctuation(bool on)
如果on 是true
,则忽略标点符号和符号;如果false
,则关注它们。
另请参见 ignorePunctuation().
void QCollator::setLocale(const QLocale &locale)
将校对器的 locale 设置为locale 。
另请参阅 locale() 。
void QCollator::setNumericMode(bool on)
当on 为true
时,启用数字排序模式。
另请参阅 numericMode() 。
QCollatorSortKey QCollator::sortKey(const QString &string) const
返回string 的排序键。
创建排序键通常比直接使用compare() 方法要慢一些。但如果要重复比较字符串(例如对整个字符串列表进行排序),通常为每个字符串创建排序键,然后使用排序键进行排序会更快。
注意: 在 Darwin 上不支持 C(又称 POSIX)语言。
[noexcept]
void QCollator::swap(QCollator &other)
用other 交换该整理器。这一操作非常快速,从未出现过故障。
bool QCollator::operator()(QStringView s1, QStringView s2) const
QCollator 可用作排序算法的比较函数。如果s1 排序在s2 之前,则返回true
,否则返回false
。
另请参见 compare() 。
bool QCollator::operator()(const QString &s1, const QString &s2) const
这是一个重载函数。
[noexcept]
QCollator &QCollator::operator=(QCollator &&other)
移动--将other 赋值给此QCollator 实例。
注意: 从移动对象other 将处于部分形成状态,其中唯一有效的操作是销毁和赋新值。
QCollator &QCollator::operator=(const QCollator &other)
将other 指定给该拼版机。
© 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.