0

我想做的是比较 2 个具有特殊字符的 QStrings(法语)

首先我从服务器收到的 json 数据保存在 txtInfo

txtInfo = "Présenter";

当我遇到这样的情况时,它不会起作用(它不会设置状态。)

  if (txtInfo == "Présenter"){
          m_appState = 8;
          m_appStateString = AppStatesArray[m_appState];
      }

else {
        m_appState = -1;
        m_appStateString = "UNKNOWN";
    }

我错过了什么?如果我想比较的不是法语而是中文怎么办?

非常感谢

4

1 回答 1

3

由于 Qt 5 QString对与之比较的字符数组operator==执行fromUtf8转换。但是如果您的源文件 (.cpp) 没有使用 utf8,您需要构建自己的 QString。

根据您的源文件 (.cpp) 编码:

UTF8:

QString compared = QString::fromUtf8("Présenter");
if (txtInfo == QString::fromUtf8("Présenter")){

本地 8 位:

QString compared = QString::fromLocal8Bit("Présenter");
if (txtInfo == QString::fromUtf8("Présenter")){

为了 100% 的正确性,不要忘记标准化你的字符串:

txtInfo = txtInfo.normalized(QString::NormalizationForm_D);
QString compared = /* the correct form for you */;
if (txtInfo == compared.normalized(QString::NormalizationForm_D)){
于 2016-10-14T08:32:17.060 回答