c++qtunicodearabic

English and arabic mixed string not ordered correctly Qt


I have a code in Qt that joins the strings john جونسون and (جيد جداً), but when I add them up, I get the answer in a wrong order, ex.:

john (جيد جداً) جونسون’


Solution

  • This has nothing to do with Qt. It is a unicode thing. Qt just adds the characters.

    The problem arises because the string starts of in LTR(left to right), with 'jhon' because that is in latin alphabet, but then when you add an arabic word to that, the first letter of that word, should be on the right, because arabic is a RTL script. This means that the last letter (represented by the last bytes) is on the left. So the place where the second string got added is - in memory - the end of the string.

    You add an Arabic string to the string, because Arabic also uses '(', and thus, you stay in RTL mode.

    So you need to explicitly mark the switchover back to LTR:

    QString ltr{"\u200e"};
    QString a {"john جونسون"};
    QString b {"(جيد جداً)"};
    std::cout << (a+ltr+b).toStdString()<< std::endl;
    

    This will add a Left to right zero width character in between, which tells, whatever is displaying your string, from that point onwards, the end of the string is on the right again. (Until it reaches the arabic characters again.)