Results 1 to 3 of 3

Thread: Qt Text Encoding and the British pound sign

  1. #1
    Join Date
    Jan 2009
    Posts
    31
    Thanks
    8
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Qt Text Encoding and the British pound sign

    Morning all,

    I'm adding a £ onto a QString for eventual printing and emailing, however I'm struggling to get Qt to do anything other than display it like so "£". I've tried toLatin1() and toUtf8() but still no joy, even when dumping with qDebug() it adds it on.

    Anyone know which text encoding method would be best for that one symbol? I'm adding it like this QString string = "Some info £" + someVariable + "some more info £" + someOtherVariable;.

    Thanks!

  2. #2
    Join Date
    Oct 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qt Text Encoding and the British pound sign

    I presume you have solved / given up on this now, so simply posting since this thread came up as the top result in google when searching the problem.

    The problem, it turns out, is one with ASCII not QT or even C++. The basic ASCII set does not include the pound symbol, but using QString as follows prints a pound symbol correctly:

    QString(163)

    I have read some threads which claim that it should be QString(156) but this didn't work for me.

  3. #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: Qt Text Encoding and the British pound sign

    The pound symbol is Unicode code point U+00A3, 163 in decimal. Using QString(163) causes an implicit conversion to a Unicode QChar using the short/int constructor so that the compiler can use it in the QString constructor.

    The two characters the OP saw are the result of treating the two bytes that U+00A3 occupies in UTF8 (\xC2 \xA3) as two separate characters in ISO8859-1 (Latin1) or a similar 8-bit encoding. Assuming the original poster was using an editor saving in UTF8 encoding this would have worked:
    Qt Code:
    1. qDebug() << QString::fromUtf8("text £%1 text").arg(1000); // literally typing the pound symbol into the string
    2. // output is
    3. "text £1000 text"
    To copy to clipboard, switch view to plain text mode 
    and been easier to understand than QString(163).

    156 is the position of the £ symbol in "Code Page 437" used by IBM in the original PC to extend the 7-bit ASCII code. AFAICT Qt has no way to convert from this encoding.
    Last edited by ChrisW67; 4th October 2012 at 04:30.

Similar Threads

  1. QSqlQuery text encoding change MySQL4 -> MySQL5
    By theophilus in forum Qt Programming
    Replies: 1
    Last Post: 15th January 2010, 13:29
  2. Character encoding in text edit and llne edit
    By greenvirag in forum Qt Programming
    Replies: 3
    Last Post: 20th January 2009, 08:45
  3. Euro sign
    By dragon in forum Qt Programming
    Replies: 3
    Last Post: 8th September 2008, 19:58
  4. Problems with text encoding
    By LMZ in forum Qt Programming
    Replies: 4
    Last Post: 3rd July 2007, 12:49

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.