本页内容

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()
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)
(since 6.12) bool operator!=(const QCollator &lhs, const QCollator &rhs)
(since 6.12) bool operator==(const QCollator &lhs, const QCollator &rhs)

详细说明

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 在 macOS 上默认使用等效的 Apple API)。但是,如果编译时 ICU 不可用或被显式禁用,Qt 将使用仅基于 POSIX API 的备用后端。该后端存在以下限制:

使用任何不受支持的选项都会在应用程序的输出中打印一条警告。

成员函数文档

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 区域设置中 ,当启用区分大小写时,所有小写字母都会排在所有大写字母之后;而在大多数其他区域设置中,每个小写字母都会排在其大写对应字母的紧前或紧后。因此,在 C 区域设置中,“Zap”排在“ape”之前,但在大多数其他区域设置中则排在“ape”之后。

另请参阅 setCaseSensitivity()。

int QCollator::compare(QStringView s1, QStringView s2) const

比较s1 与s2 。

如果s1 小于s2 ,则返回一个负整数;如果大于s2 ,则返回一个正整数;如果两者相等,则返回零。

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 ,则返回一个正整数;如果两者相等,则返回零。

注意:在 Qt 6.4 之前的版本中,长度参数的类型为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()。

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 分配给此排序器。

相关的非成员

[noexcept, since 6.12] bool operator!=(const QCollator &lhs, const QCollator &rhs)

[noexcept, since 6.12] bool operator==(const QCollator &lhs, const QCollator &rhs)

如果 `lhs ` 和 `rhs ` 使用相同的区域设置和排序规则选项,则返回 `true `;否则返回 `false`。

这些函数首次出现在 Qt 6.12 中。

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