0

What I am trying to do is comparin 2 QStrings that have special characters (French)

first I recieved from server as json data saved in txtInfo

txtInfo = "Présenter";

When I am having condition like this it's not gonna work(its not gonna set state.)

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

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

What I am missing? What if I would like to compare not French but Chinese?

Thank you very much

Dušan Tichý
  • 498
  • 1
  • 7
  • 21
  • Have you tried to use the debugger to view the inner content of these string? – Danh Oct 14 '16 at 08:43
  • @Danh yes, its what it shoud be "Présenter" thanks thou – Dušan Tichý Oct 14 '16 at 08:43
  • This discussion might be interesting because the string encoding in your code might also play a role: [Using Unicode in C++ source code](http://stackoverflow.com/questions/331690/using-unicode-in-c-source-code) – Rumpel Oct 14 '16 at 08:53

1 Answers1

3

Since Qt 5 QString's operator== performs fromUtf8 conversion on the character array being compared to it. But if your source file (.cpp) isn't using utf8 you need to build your own QString.

Depending on your source file's (.cpp) encoding:

Utf8:

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

local 8-bit:

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

For 100% correctness, don't forget to normalize your strings:

txtInfo = txtInfo.normalized(QString::NormalizationForm_D);
QString compared = /* the correct form for you */;
if (txtInfo == compared.normalized(QString::NormalizationForm_D)){
krzaq
  • 16,240
  • 4
  • 46
  • 61
  • 1
    I don't think `QString::fromUtf8` will help. [QString::operator==](http://doc.qt.io/qt-5/qstring.html#operator-eq-eq-1) said that: The other byte array is converted to a QString using the fromUtf8() function. – Danh Oct 14 '16 at 08:36
  • @Danh I recall having this exact problem fixed by doing this back in the Qt 4 days. But it seems you're right. – krzaq Oct 14 '16 at 08:38
  • Normalization is critical. Choosing the *right* normalization is also important. – Martin Bonner supports Monica Oct 14 '16 at 08:39
  • @MartinBonner I'm far from a unicode expert. Any insights on which normalization is important? (I was under assumption that it doesn't really matter, as long it's the same on both sides) – krzaq Oct 14 '16 at 08:39
  • 1
    @krzaq in the old day, The other const char pointer is converted to a QString using the fromAscii() function. as stated [here](http://doc.qt.io/qt-4.8/qstring.html#operator-eq-eq-4) And this question is tagged Qt 5.7 – Danh Oct 14 '16 at 08:39
  • yep, it doesnt work... and it's already in utf because i used something like this: QJsonDocument jsonDocument = QJsonDocument::fromJson(s.toUtf8(), &jsonParseError); – Dušan Tichý Oct 14 '16 at 08:41
  • What is your source file's encoding? – krzaq Oct 14 '16 at 08:44
  • @aladin8848 He meant the encoding of cpp source file – Danh Oct 14 '16 at 08:45
  • @aladin8848 if you're not sure, it should be displayed by Qt Creator or the editor of your choice: http://i.imgur.com/wfHcJnZ.png – krzaq Oct 14 '16 at 08:47
  • Wel, I am looking for what encoding I have, I have Visual Studio 2013, something like what you @krzaq showed in screenshot i dont have – Dušan Tichý Oct 14 '16 at 08:53
  • @aladin8848 [this](http://stackoverflow.com/questions/696627/how-to-set-standard-encoding-in-visual-studio) might be an interesting discussion for you. Try `fromLocal8Bit` first, though – krzaq Oct 14 '16 at 08:56
  • 1
    @aladin8848 I keep a habit to always save file in UTF-8 w/o BOM encoding – Danh Oct 14 '16 at 08:57
  • thx guys for help. this helped : QString::fromLocal8Bit("Présenter"); – Dušan Tichý Oct 14 '16 at 09:01
  • @krzaq: Me neither, the important question is do you want "canonical equivalence" or "compatibility equivalence"? The OP must decide that for themselves (on a case by case basis). – Martin Bonner supports Monica Oct 14 '16 at 09:01