PDA

View Full Version : Problem in reading Arabic string from QString::fromUtf8



kishore7771
31st July 2013, 17:17
Passing an Arabic string and collecting in QString using QString::fromUtf8.




QString temp = QString::fromUtf8( "الجدÙÅ Øà ‚¯ : 12");
QString temp1 = QString::fromUtf8( "الجدÙÅ Øà ‚¯ : ABC");


temp contains "الجديد : 12"
temp1 contains "الجديد : ABA"
Due to this while displaying some Name: Value pair is not displaying properly.[B:A is displayed instead of A:B]
Source is not under control to send in reverse order all the time.
Tried to prepare the string by splitting based on : and reconstructing, still it is same.
Is it possible to control left alignment property while copying arabic string if it contains only Arabic and numbers?

Any help would be appreciated.

Thanks,
Kishore

ChrisW67
31st July 2013, 23:47
For those wanting to reproduce:


// U+0627 U+0644 U+062C U+062F U+064A U+062F
QString temp = QString::fromUtf8( "\xD8\xA7\xD9\x84\xD8\xAC\xD8\xAF\xD9\x8A\xD8\xAF : 12");
QString temp1 = QString::fromUtf8( "\xD8\xA7\xD9\x84\xD8\xAC\xD8\xAF\xD9\x8A\xD8\xAF : ABC");
qDebug() << temp;
qDebug() << temp1;

Output in a terminal support bi-directional text:


"الجديد : 12"
"الجديد : ABC"


Both strings report isRightToLeft() == true.

Despite the characters in the string being in precisely the order of the UTF8 input, splitting on " : " gives QStringLists in opposite orders, which I think is the OP's problem:


"الجديد" " 12"
"الجديد" " ABC"

Interestingly the first string in each pair reports isRightToLeft() and the second does not.

This works but is limited to single character separators:


QByteArray raw1( "\xD8\xA7\xD9\x84\xD8\xAC\xD8\xAF\xD9\x8A\xD8\xAF : 123");
QList<QByteArray> parts1 = raw1.split(':');
foreach (const QByteArray &part, parts1)
qDebug() << QString::fromUtf8(part);

QByteArray raw2( "\xD8\xA7\xD9\x84\xD8\xAC\xD8\xAF\xD9\x8A\xD8\xAF : ABC");
QList<QByteArray> parts2 = raw2.split(':');
foreach (const QByteArray &part, parts2)
qDebug() << QString::fromUtf8(part);


I don't have enough RTL-foo to see a QString answer.

kishore7771
1st August 2013, 11:55
Thanks for the response.
But i need to combine these two string and display on one label.
When ever a number is appended to Arabic string format is getting misaligned.
There is no method in QT to copy string and set the Layout Option like QTextOption::setTextDirection.

ChrisW67
2nd August 2013, 09:48
QString follows the convoluted Unicode rules for bidirectional text. The RTL Arabic text is followed by a neutral direction space, weakly directional colon, weakly directional digits, and strong directional latin alphabet chars. The neutral and weak characters tend to inherit the directionality from preceding characters and the "12" ends up left of the Arabic text where the "ABC" does not.

You have special characters you can use to control RTL/LTR when the automatically inferred behaviour fails. Inserting a LRM (0x200E) before the first space in either string (or the concatenation of both) should do the trick:


QString temp = QString::fromUtf8( "\xD8\xA7\xD9\x84\xD8\xAC\xD8\xAF\xD9\x8A\xD8\xAF : 12");
temp = temp.insert(6, QChar(0x200e));
QPushButton p(temp.append(temp));
p.show();

However, QLabel and QPushButton seem to do this differently.