[Interest] Converting std::string.c_str() code to use QString methods

Thiago Macieira thiago.macieira at intel.com
Sat Aug 31 05:42:43 CEST 2013

On sexta-feira, 30 de agosto de 2013 19:47:48, Syam wrote:
> On 31-Aug-2013 6:28 AM, "Thiago Macieira" <thiago.macieira at intel.com> wrote:
> > On sexta-feira, 30 de agosto de 2013 19:17:58, Scott Aron Bloom wrote:
> > > I use toUtf8()... Though it wouldn't surprise me if Thiago scolds me
> > > for it
> >
> > If you're on Unix, that's fine. It's fine because if your Unix system
> > uses a
> > locale different from UTF-8, you *really* should be scolded :-)
> On Linux, whenever I need a const char* from a QString, I always do
> toLatin1().constData().
> Earlier I used toAscii(), but then some discussion on Qt5 suggested that
> toLatin1() is more appropriate.

toAscii() and toLatin1() are most often the same. Except when they aren't -- 
that's when someone called setCodecForCStrings. And then toAscii() is no 
longer doing ASCII, it's doing something potentially incompatible with ASCII.

That's why this function is deprecated in Qt 5. In Qt 5, it's documented to do 
US-ASCII only and it has undefined behaviour if the QString contains characters 
not encodable in US-ASCII.

> Am I right here? Of course, the assumption is that the original QString is
> simple English text.

Even “simple English” may contain characters not encodable in Latin 1…

As I've just proven :-)

Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20130830/094b3b89/attachment.sig>

More information about the Interest mailing list