QCollator Class

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=(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

collator 大文字小文字の区別を返します。

設定するまで、デフォルトは大文字小文字を区別します。

注意: 大文字小文字を区別する場合、C ロケールではすべての小文字がすべての大文字の後にソートされます。したがって、"Zap "はCロケールでは "ape "の前にソートされるが、他のほとんどのロケールでは後にソートされる。

setCaseSensitivity()も参照のこと

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

s1s2 を比較する。

s1s2 より小さい場合は負の整数を、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

これはオーバーロードされた関数である。

s1s2 を比較する。len1len2 は、s1s2 が指すQChar 配列の長さを指定する。

s1s2 より小さい場合は負の整数を、s2 より大きい場合は正の整数を、等しい場合はゼロを返します。

注意: Qt 6.4より前のバージョンでは、length引数の型はqsizetype ではなくint でした。

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

文字列s1s2 を比較し、ソート順を返す。この関数は、デフォルトで構築された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)

ontrue の場合、句読点と記号を無視し、false の場合、句読点と記号に注意を払う。

ignorePunctuation()も参照

void QCollator::setLocale(const QLocale &locale)

照合器のロケールをlocale に設定する。

locale()も参照

void QCollator::setNumericMode(bool on)

ontrue のとき、数値ソートモードを有効にする。

numericMode()も参照

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

string の sortKey を返す。

ソートキーの作成は、compare() メソッドを直接使うよりも多少遅くなります。しかし、文字列を繰り返し比較する場合 (たとえば文字列のリスト全体をソートする場合など) は、文字列ごとにソートキーを作成し、そのキーを使ってソートするほうが高速になります。

注意: DarwinのC(POSIX)ロケールではサポートされていません。

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

このコレーターをother と交換する。この操作は非常に速く、失敗することはない。

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

QCollator は、ソートアルゴリズムの比較関数として使用できる。この関数は、s1s2 よりも先にソートされていればtrue を返し、そうでなければfalse を返す。

compare()も参照のこと

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

これはオーバーロードされた関数である。

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

Move-assignother をこの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.