好的,我们来整理一下 Qt 学习记录中关于 Qt::CaseSensitive 的内容,帮你快速理解它的用途和示例。


1️⃣ 基本概念

在 Qt 中,Qt::CaseSensitivity 是一个枚举,用于指定 字符串比较是否区分大小写

enum Qt::CaseSensitivity {
    Qt::CaseSensitive,    // 区分大小写
    Qt::CaseInsensitive   // 不区分大小写
};

  • Qt::CaseSensitiveAa
  • Qt::CaseInsensitiveA = a

2️⃣ 常见使用场景

  1. 字符串比较
QString str1 = "Hello";
QString str2 = "hello";

if (str1.compare(str2, Qt::CaseSensitive) == 0) {
    qDebug() << "相等";
} else {
    qDebug() << "不相等";  // 输出:不相等
}

if (str1.compare(str2, Qt::CaseInsensitive) == 0) {
    qDebug() << "相等";  // 输出:相等
}

  1. 查找子字符串
QString text = "QtCaseSensitiveExample";

int index1 = text.indexOf("case", 0, Qt::CaseSensitive);    // 返回 -1(找不到)
int index2 = text.indexOf("case", 0, Qt::CaseInsensitive);  // 返回 2(找到)

qDebug() << index1 << index2;

  1. 排序QStringList::sort() 支持自定义比较):
QStringList list = {"banana", "Apple", "apple", "Banana"};
std::sort(list.begin(), list.end(), [](const QString &a, const QString &b) {
    return QString::localeAwareCompare(a, b, Qt::CaseInsensitive) < 0;
});


3️⃣ 小结

  • Qt::CaseSensitive → 区分大小写
  • Qt::CaseInsensitive → 不区分大小写
  • 使用场景主要有:
    • 字符串比较 (compare)
    • 查找 (indexOf, contains)
    • 排序
    • 正则表达式匹配(QRegExp、QRegularExpression 也可以指定大小写敏感)

💡 记忆技巧
Qt::CaseSensitive → “大小写敏感的 Case” → A ≠ a


如果你愿意,我可以帮你整理一张 Qt 字符串操作 vs CaseSensitivity 对照表,让 compareindexOfcontains、排序等常用方法对大小写敏感/不敏感的效果一目了然。

你希望我画这个表吗?