Results 1 to 4 of 4

Thread: Putting unicode in QString literals

  1. #1
    Join Date
    Sep 2008
    Location
    Delft, Netherlands
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Putting unicode in QString literals

    Hi all,

    Is there a convenient way to put Unicode characters in a QString literal?

    The best I've come up with so far is something like this:

    const QChar euroSymbol(0x20ac);
    const QString somestring = QString("The Euro symbol is: '") + euroSymbol + Qstring("'.");

    The problem with this is that it is rather unclear, and that it cannot nicely be subjected to tr() for internationalization.

    This is the one case I yearn for Java (where you can put \u escape codes in a string) although I realize Qt is not to blame for C++ shortcomings in this area.

    Regards, Sidney

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Putting unicode in QString literals

    you can encode your files in utf8 and then you can directly write €! therefore you must also use trUtf8(). Instead of using "+" to concat the string use e.g. tr("dsf %1 ...").arg(...) or QString("...").arg()

  3. #3
    Join Date
    Jul 2009
    Location
    Italy, Pieve Ligure (GE)
    Posts
    55
    Thanks
    7
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Putting unicode in QString literals

    Quote Originally Posted by reddish View Post
    This is the one case I yearn for Java (where you can put \u escape codes in a string) although I realize Qt is not to blame for C++ shortcomings in this area.
    Well, it is not a C++ shortcoming. C++ allows the following declaration:
    Qt Code:
    1. const wchar_t somestring = L"The Euro symbol is: \x20ac.";
    To copy to clipboard, switch view to plain text mode 
    A bit cumbersome is that, to have a Qt QString from it, you have to use the QString::fromWCharArray() function:
    Qt Code:
    1. [...]->setText(QString::fromWCharArray(somestring) );
    To copy to clipboard, switch view to plain text mode 
    which of course cannot be put in the declaration:
    Qt Code:
    1. const QString somestring = QString::fromWCharArray(L"The Euro symbol is: \x20ac."); // not legal!
    To copy to clipboard, switch view to plain text mode 
    A QString constructor accepting a wchar_t[] as parameter could be useful...

    M.

    P.S.: if the characters following the \x escape sequence could be interpreted as hex digits, the wchar_t string must be split:
    Qt Code:
    1. const wchar_t somestring = L"The Euro symbol is: \x20ac" "ABCD";
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Oct 2012
    Posts
    1
    Qt products
    Qt/Embedded
    Platforms
    Maemo/MeeGo

    Default Re: Putting unicode in QString literals

    Quote Originally Posted by Lykurg View Post
    you can encode your files in utf8 and then you can directly write €! therefore you must also use trUtf8(). Instead of using "+" to concat the string use e.g. tr("dsf %1 ...").arg(...) or QString("...").arg()
    Thank you.

Similar Threads

  1. Qy 4.4.3 MySQL driver failed
    By pamalite in forum Installation and Deployment
    Replies: 2
    Last Post: 23rd January 2010, 01:09
  2. File rename detection
    By bunjee in forum Qt Programming
    Replies: 6
    Last Post: 23rd July 2009, 15:22
  3. Custom Model Advice Requested
    By mclark in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2008, 16:26
  4. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 17:59
  5. Converting QString to unsigned char
    By salston in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 22:10

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.