PDA

View Full Version : Qt Text Encoding and the British pound sign



NicholasSmith
26th August 2010, 10:58
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!

SpyMasterMatt
4th October 2012, 01:54
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.

ChrisW67
4th October 2012, 03:22
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:


qDebug() << QString::fromUtf8("text £%1 text").arg(1000); // literally typing the pound symbol into the string
// output is
"text £1000 text"

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.