[Qt-interest] Fwd: Convert a QString to string
ami guru
dosto.walla at gmail.com
Tue Feb 10 17:09:20 CET 2009
Hello Thiago,
What if we are using some third party API that uses the std::string instad
of the std::wstring
The third party API has function that accept string instead
Any suggestion on that?
Regards
Sajjad
---------- Forwarded message ----------
From: Thiago Macieira <thiago.macieira at trolltech.com>
Date: Tue, Feb 10, 2009 at 4:00 PM
Subject: Re: [Qt-interest] Convert a QString to string
To: qt-interest at trolltech.com
Em Terça-feira 10 Fevereiro 2009, às 15:05:18, Jan Ekholm escreveu:
> On Tuesday 10 February 2009, Sujan Dasmahapatra wrote:
> > How can I convert a QString str1 to string str2.
>
> Assuming you mean std::string it's pretty obvious if you actually look
into
> the docs a bit...
>
> std::string QString::toStdString () const
Please be careful with that function.
std::string is not a string (in the Qt sense), but a byte array. It's closer
to QByteArray than to QString. That means that, when you convert from
QString
to std::string, you're introducing an encoding conversion and you should,
therefore, be very careful.
Unlike the conversions to QByteArray, QString offers no choice of encoding
for
the conversion to std::string. Your only option is the toAscii() conversion,
which is Latin 1 by default but can be changed with
QTextCodec::setCodecForCStrings. This is important, because it's not the
locale conversion: you may be losing data by converting to std::string.
My recommendation is that you instead modify the rest of your code to use
QString, as far as you can.
If you can't, then try to use std::wstring instead: all platforms where Qt
is
supported use UTF-16 or UCS-4 for wchar_t, so there's the conversion is
trivial and there's no loss of data.
--
Thiago Macieira - thiago.macieira (AT) nokia.com
Senior Product Manager - Nokia, Qt Software
Sandakerveien 116, NO-0402 Oslo, Norway
_______________________________________________
Qt-interest mailing list
Qt-interest at trolltech.com
http://lists.trolltech.com/mailman/listinfo/qt-interest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090210/c7271e69/attachment.html
-------------- next part --------------
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
iD8DBQBJkZaIM/XwBW70U1gRAhmsAJ4q9R1uy/bG0iVMuQU8dc2+C1uuqQCeJ9OW
o6hZh5/ToYFbFR0riSB+nco=
=iDlh
-----END PGP SIGNATURE-----
More information about the Qt-interest-old
mailing list