QCollator 类

QCollator 类根据本地整理算法比较字符串。 更多...

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

注意: 此类的所有函数 可重入 .

公共函数

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= (const QCollator & other )
QCollator & operator= (QCollator && other )

静态公共成员

(since 6.3) int defaultCompare (QStringView s1 , QStringView s2 )
(since 6.3) QCollatorSortKey defaultSortKey (QStringView key )

详细描述

QCollator 被初始化采用 QLocale . It can then be used to compare and sort strings by using the ordering appropriate for that locale.

A QCollator object can be used together with template-based sorting algorithms, such as std::sort(), to sort a list with QString 条目。

QStringList sortedStrings(QStringList seq)
{
    QCollator order;
    std::sort(seq.begin(), seq.end(), order);
    return seq;
}
					

In addition to the locale, several optional flags can be set that influence the result of the collation.

POSIX fallback implementation

On Unix systems, Qt is normally compiled to use ICU (except for macOS, where Qt defaults to using an equivalent Apple API). However, if ICU was not available at compile time or explicitly disabled, Qt will use a fallback backend that uses the POSIX API only. This backend has several limitations:

The use of any of the unsupported options will cause a warning to be printed to the application's output.

成员函数文档编制

QCollator:: QCollator ()

Constructs a QCollator using the default locale's collation locale.

The system locale, when used as default locale, may have a collation locale other than itself (e.g. on Unix, if LC_COLLATE is set differently to LANG in the environment). All other locales are their own collation locales.

另请参阅 setLocale (), QLocale::collation (),和 QLocale::setDefault ().

[explicit] QCollator:: QCollator (const QLocale & locale )

Constructs a QCollator using the given locale .

另请参阅 setLocale ().

QCollator:: QCollator (const QCollator & other )

创建副本为 other .

[noexcept] QCollator:: QCollator ( QCollator && other )

Move constructor. Moves from other into this collator.

Note that a moved-from QCollator can only be destroyed or assigned to. The effect of calling other functions than the destructor or one of the assignment operators is undefined.

[noexcept] QCollator:: ~QCollator ()

Destroys this collator.

Qt::CaseSensitivity QCollator:: caseSensitivity () const

Returns case sensitivity of the collator.

This defaults to case-sensitive until set.

注意: In the C locale, when case-sensitive, all lower-case letters sort after all upper-case letters, where most locales sort each lower-case letter either immediately before or immediately after its upper-case partner. Thus "Zap" sorts before "ape" in the C locale but after in most others.

另请参阅 setCaseSensitivity ().

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

比较 s1 with s2 .

Returns a negative integer if 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 with s2 . len1 and len2 specify the lengths of the QChar arrays pointed to by s1 and s2 .

Returns a negative integer if s1 小于 s2 ,正整数若大于 s2 ,和 0 若它们相等。

注意: In Qt versions prior to 6.4, the length arguments were of type int , not qsizetype .

[static, since 6.3] int QCollator:: defaultCompare ( QStringView s1 , QStringView s2 )

Compares the strings s1 and s2 , returning their sorting order. This function performs the same operation as compare () on a default-constructed QCollator 对象。

该函数在 Qt 6.3 引入。

另请参阅 compare () 和 defaultSortKey ().

[static, since 6.3] QCollatorSortKey QCollator:: defaultSortKey ( QStringView key )

Returns the sort key for the string key . This function performs the same operation as sortKey () on a default-constructed QCollator 对象。

该函数在 Qt 6.3 引入。

另请参阅 sortKey () 和 defaultCompare ().

bool QCollator:: ignorePunctuation () const

Returns whether punctuation and symbols are ignored when collating.

true , strings are compared as if all punctuation and symbols were removed from each string.

另请参阅 setIgnorePunctuation ().

QLocale QCollator:: locale () const

Returns the locale of the collator.

Unless supplied to the constructor or by calling setLocale (), the system's default collation locale is used.

另请参阅 setLocale () 和 QLocale::collation ().

bool QCollator:: numericMode () const

返回 true if numeric sorting is enabled, false 否则。

true , numerals are recognized as numbers and sorted in arithmetic order; for example, 100 sortes after 99. When false , numbers are sorted in lexical order, so that 100 sorts before 99 (because 1 is before 9). By default, this option is disabled.

另请参阅 setNumericMode ().

void QCollator:: setCaseSensitivity ( Qt::CaseSensitivity cs )

Sets the case-sensitivity of the collator to cs .

另请参阅 caseSensitivity ().

void QCollator:: setIgnorePunctuation ( bool on )

Ignores punctuation and symbols if on is true , attends to them if false .

另请参阅 ignorePunctuation ().

void QCollator:: setLocale (const QLocale & locale )

Sets the locale of the collator to locale .

另请参阅 locale ().

void QCollator:: setNumericMode ( bool on )

Enables numeric sorting mode when on is true .

另请参阅 numericMode ().

QCollatorSortKey QCollator:: sortKey (const QString & string ) const

Returns a sortKey for string .

Creating the sort key is usually somewhat slower, than using the compare () methods directly. But if the string is compared repeatedly (e.g. when sorting a whole list of strings), it's usually faster to create the sort keys for each string and then sort using the keys.

注意: Not supported with the C (a.k.a. POSIX) locale on Darwin.

[noexcept] void QCollator:: swap ( QCollator & other )

Swaps this collator with other 。此函数非常快且从不失败。

bool QCollator:: operator() ( QStringView s1 , QStringView s2 ) const

A QCollator can be used as the comparison function of a sorting algorithm. It returns true if s1 sorts before s2 , otherwise false .

另请参阅 compare ().

bool QCollator:: operator() (const QString & s1 , const QString & s2 ) const

这是重载函数。

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

赋值 other to this collator.

[noexcept] QCollator &QCollator:: operator= ( QCollator && other )

Move-assigns from other to this collator.

Note that a moved-from QCollator can only be destroyed or assigned to. The effect of calling other functions than the destructor or one of the assignment operators is undefined.