[Qt-interest] Showing UTF-8 strings

Thiago Macieira thiago.macieira at trolltech.com
Sat Aug 22 09:17:10 CEST 2009


M. Bashir Al-Noimi wrote:
>Hi All,
>
>Why Qt can't show UTF-8 strings which written in the source code?

Qt can. Since I know Qt can and you proved that the string didn't appear, 
we must conclude that there's something wrong in your code.

Let's look at your code:

    QMessageBox::information(this, "مرحبا", "السلام عليكم");

That's http://doc.trolltech.com/4.5/qmessagebox.html#information. The 
second parameter is a QString. So you're using this constructor: 
http://doc.trolltech.com/4.5/qstring.html#QString-7.

That constructor's documentation is:

>Constructs a string initialized with the ASCII string str. The given
> const char pointer is converted to Unicode using the fromAscii()
> function.

And fromAscii's documentation is 
(http://doc.trolltech.com/4.5/qstring.html#fromAscii):

>Returns a QString initialized with the first size characters of the
> 8-bit ASCII string str. If size is -1 (default), it is taken to be
> qstrlen(str).
>If a codec has been set using QTextCodec::setCodecForCStrings(), it is
> used to convert str to Unicode; otherwise this function does the same
> as fromLatin1().

The second paragraph is important. Did you set a codec for C strings? If 
you didn't (and the fact that you got mojibake seems to indicate that), 
QString is applying QString::fromLatin1 to your literal.

I went over this yesterday in the "Accented characters and QDom in 
Windows" thread. I urge people to pay attention to other threads that are 
happening in the list. Especially when they are related ("accented 
characters" should have grabbed your attention).

So, if you have string problems, recompile your app with 
QT_NO_CAST_FROM_ASCII and QT_NO_CAST_TO_ASCII

-- 
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/20090822/89a00f7a/attachment.bin 


More information about the Qt-interest-old mailing list