Results 1 to 4 of 4

Thread: Problem in reading Arabic string from QString::fromUtf8

  1. #1
    Join Date
    Oct 2009
    Posts
    23
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    2

    Default Problem in reading Arabic string from QString::fromUtf8

    Passing an Arabic string and collecting in QString using QString::fromUtf8.

    Qt Code:
    1. QString temp = QString::fromUtf8( "الجديد : 12");
    2. QString temp1 = QString::fromUtf8( "الجديد : ABC");
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Problem in reading Arabic string from QString::fromUtf8

    For those wanting to reproduce:
    Qt Code:
    1. // U+0627 U+0644 U+062C U+062F U+064A U+062F
    2. QString temp = QString::fromUtf8( "\xD8\xA7\xD9\x84\xD8\xAC\xD8\xAF\xD9\x8A\xD8\xAF : 12");
    3. QString temp1 = QString::fromUtf8( "\xD8\xA7\xD9\x84\xD8\xAC\xD8\xAF\xD9\x8A\xD8\xAF : ABC");
    4. qDebug() << temp;
    5. qDebug() << temp1;
    To copy to clipboard, switch view to plain text mode 
    Output in a terminal support bi-directional text:
    Qt Code:
    1. "الجديد : 12"
    2. "الجديد : ABC"
    To copy to clipboard, switch view to plain text mode 

    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:
    Qt Code:
    1. "الجديد" " 12"
    2. "الجديد" " ABC"
    To copy to clipboard, switch view to plain text mode 
    Interestingly the first string in each pair reports isRightToLeft() and the second does not.

    This works but is limited to single character separators:
    Qt Code:
    1. QByteArray raw1( "\xD8\xA7\xD9\x84\xD8\xAC\xD8\xAF\xD9\x8A\xD8\xAF : 123");
    2. QList<QByteArray> parts1 = raw1.split(':');
    3. foreach (const QByteArray &part, parts1)
    4. qDebug() << QString::fromUtf8(part);
    5.  
    6. QByteArray raw2( "\xD8\xA7\xD9\x84\xD8\xAC\xD8\xAF\xD9\x8A\xD8\xAF : ABC");
    7. QList<QByteArray> parts2 = raw2.split(':');
    8. foreach (const QByteArray &part, parts2)
    9. qDebug() << QString::fromUtf8(part);
    To copy to clipboard, switch view to plain text mode 

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

  3. #3
    Join Date
    Oct 2009
    Posts
    23
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    2

    Default Re: Problem in reading Arabic string from QString::fromUtf8

    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.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Problem in reading Arabic string from QString::fromUtf8

    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:
    Qt Code:
    1. QString temp = QString::fromUtf8( "\xD8\xA7\xD9\x84\xD8\xAC\xD8\xAF\xD9\x8A\xD8\xAF : 12");
    2. temp = temp.insert(6, QChar(0x200e));
    3. QPushButton p(temp.append(temp));
    4. p.show();
    To copy to clipboard, switch view to plain text mode 
    However, QLabel and QPushButton seem to do this differently.

  5. The following user says thank you to ChrisW67 for this useful post:

    kishore7771 (29th August 2013)

Similar Threads

  1. Arabic language support problem (right to left)
    By sam_er83 in forum Qt Tools
    Replies: 1
    Last Post: 14th May 2012, 09:48
  2. Replies: 1
    Last Post: 14th May 2011, 08:02
  3. QString::fromUtf8 problems converting micro sign
    By smacchia in forum Qt Programming
    Replies: 3
    Last Post: 15th February 2011, 22:07
  4. Arabic problem with Qt app Windows Mobile
    By mms in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 20th December 2010, 15:51
  5. Replies: 8
    Last Post: 25th November 2010, 11:40

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.