[Qt-interest] Accented characters and QDom in Windows

Thiago Macieira thiago.macieira at trolltech.com
Fri Aug 21 08:45:19 CEST 2009


Ellen Kestrel wrote:
>The issue with the special characters not showing isn't (necessarily)
>related to inputs from files or hard-coded strings; if the user types
> (or copies and pastes) the character into a line edit widget or
> similar, it shows up as a line or a box in the line edit.

Hi Ellen

There's something wrong with your code. I urge you to compile with 
QT_NO_CAST_TO_ASCII and QT_NO_CAST_FROM_ASCII so you can find it.

When the user pastes something into the line edit, he/she can only paste 
valid Unicode strings. Therefore, there's nothing mis-encoded. If 
something shows up mis-encoded, it's because your program corrupted it.

The only sources of bad encoding are conversion from 8-bit into Unicode. 
They can be either the source file's hard-coded strings, or not properly 
setting the encoding of a given file. Or, which is your case, mishandling 
of already QString objects.

> Also, the
> fromXXX functions all take character arrays, so while they do work with
> hard-coded text, the user input is already in QString form by the time
> I get it.

That's wrong. Never use fromXXX on something that comes from QString. For 
example, the following code will likely produce bad data:

	QString str = QString::fromUtf8(lineedit.text());

If you add the macros I mentioned, this code will not compile. You'll 
notice that, to make it compile again, you have to either fix the error 
(which is the recommended alternative :-) ) or make the error more 
explicit:

	QString str = QString::fromUtf8(lineedit.text().toAscii()));

As you can see above, you take some QString, encode it to ASCII[*], then 
decode it as UTF-8. That can't be right. The recommended alternative is to 
simply do:

	QString str = lineedit.text();

[*] "ASCII" is a misnomer here; it's actually what Qt calls the "codec for 
C strings", that is, the encoding that your source file should be. By 
default, that's Latin 1.

-- 
Thiago Macieira - thiago.macieira (AT) nokia.com
  Senior Product Manager - Nokia, Qt Development Frameworks
     Sandakerveien 116, NO-0402 Oslo, Norway

Qt Developer Days 2009 | Registration Now Open!
Munich, Germany: Oct 12 - 14     San Francisco, California: Nov 2 - 4
      http://qt.nokia.com/qtdevdays2009
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090821/c75d195c/attachment.bin 


More information about the Qt-interest-old mailing list