[Qt-interest] QString to TCHAR* and back

Thiago Macieira thiago at kde.org
Wed Feb 24 20:58:18 CET 2010


Em Quarta-feira 24 Fevereiro 2010, às 17:17:35, Duane Hebert escreveu:
> What is the recommend way to do these conversions?
> 
> If found this on the net and it seems to work:
> #ifdef UNICODE
> #define QStringToTCHAR(x)     (wchar_t*) x.utf16()
> #define TCHARToQString(x)     QString::fromUtf16((x))
> #else
> #define QStringToTCHAR(x)     x.local8Bit().constData()
> #define TCHARToQString(x)     QString::fromLocal8Bit((x))
> #endif
> 
> I'm using Qt 4.5.1 win commercial.
> 

Since QChar is always 2 bytes in size, which is the size of wchar_t on 
Windows, you can simply (reinterpret) cast from QChar to wchar_t/ushort and 
back.

Qt does it all over the place. You can use the QString constructor that takes 
QChar* as well as .data().

A note of warning on the local8Bit().constData() above: you're taking the 
address of a temporary.

Finally, note that Qt no longer supports the DOS-based Windows versions. Do 
yourself a favour and stop building code that is not Unicode (wide-char).

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Senior Product Manager - Nokia, Qt Development Frameworks
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100224/506342d6/attachment.bin 


More information about the Qt-interest-old mailing list