Results 1 to 3 of 3

Thread: Any way to put Unicode character in QString without using QString("%1").arg(QChar)?

  1. #1
    Join Date
    May 2013
    Posts
    2
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Any way to put Unicode character in QString without using QString("%1").arg(QChar)?

    Is it possible to specify QStrings which are texts containing extended ASCII accented characters, coded as Unicode, and have these correctly rendered when output using qDebug()? Eg., create a QString with the text "Texte en français" as QString("Texte en fran\u00C7ais") and have it show up, say when printed with qDebug(), as "Texte en français".

    I know that if I get to the QString using QString::fromLocal8Bit("Texte en français"), it will work--the output text is "Texte en français", and I can use this approach if necessary. I'm just wondering why, if I do QString("Texte en fran\u00C7ais"), it doesn't work--I get an output of "Texte en franÇais" with the à followed by a small square containing what appears to be an 00 87.

    Note, this isn't an internationalization situation, where I should be using tr(), lupdate, Qt Linguist, lrelease, and so on. I'm just curious as to whether the thing can be done.

  2. #2
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Any way to put Unicode character in QString without using QString("%1").arg(QChar

    It's a problem of qDebug(), not of QString. The QString handles Unicode correctly. The literals like "Texte en français" will be Unicode "Texte en français" in QString. The "Texte en fran\u00C7ais" will not work in QString, the QString does not know the "\unnnn" escapes and will put "\unnnn" in the QString instead of "nnnn" converted to int.

    qDebug() seems to use code pages internally even if the operating system is Unicode (like Linux).

  3. The following user says thank you to Radek for this useful post:

    oddity (13th November 2013)

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

    Default Re: Any way to put Unicode character in QString without using QString("%1").arg(QChar

    Your C++ compiler will convert the character literal '\u00c7' to the two UTF8 bytes 0xC3 0x87. That is what will be in the C-string that you construct the QString from.

    The Qt4 QString(const char *) constructor will use fromAscii()/fromLatin1() and misinterpret those bytes resulting in two characters in the QString where there should only be one. What you want is this:
    Qt Code:
    1. QString s(QString::fromUtf8("Texte en fran\u00e7ais"));
    2. // or
    3. QString s = QString::fromUtf8("Texte en fran\u00e7ais");
    To copy to clipboard, switch view to plain text mode 

    The Qt5 QString(const char *) constructor will use fromUtf8() so this should be fine:
    Qt Code:
    1. QString s("Texte en fran\u00e7ais");
    To copy to clipboard, switch view to plain text mode 

    On Linux qDebug() will generally do the right thing with strings constructed this way.

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

    oddity (13th November 2013)

Similar Threads

  1. Replies: 10
    Last Post: 17th July 2014, 11:52
  2. Replies: 1
    Last Post: 14th May 2011, 09:02
  3. Replies: 28
    Last Post: 22nd February 2010, 11:27
  4. Need definedInHeader("QString") == "q<somewhere>.h"
    By muenalan in forum Qt Programming
    Replies: 6
    Last Post: 29th September 2009, 12:04
  5. Error "QString::arg: Argument missing"
    By Lawand in forum Qt Programming
    Replies: 3
    Last Post: 18th February 2009, 21:26

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.