[Qt-interest] QString to TCHAR* and back
Thiago Macieira
thiago at kde.org
Wed Feb 24 21:41:56 CET 2010
Em Quarta-feira 24. Fevereiro 2010, às 21.26.48, Duane Hebert escreveu:
> >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().
>
> Not sure if I understand you exactly.
> My projects use Unicode and QString. I have a library that I need to use
> that uses const TCHAR* const to pass a port name to a ModbusRTU lib:
>
> int startupServer(const TCHAR * const portName,long baudRate, int
> dataBits,int stopBits, int parity);
>
>
> While your suggestion works (as well as the other suggestions here) as far
> as I can see with my debugger, when I pass the string ("COM1") to my
> connect function it doesn't work. It seems to me that it doesn't handle
> the nulls in the TCHAR though I can say that for sure.
>
> If I do this:
>
> QString portName("COM1");
> std::string Buffer(port.toStdString());
>
> startupServer(reinterpret_cast<const TCHAR*>(Buffer.c_str()),...
>
> This works whether I set my build options (VS2005) to Unicode or multibyte
> char.
>
> Buf if I try
> reinterpret_cast<const TCHAR*>(portName.constData())
> reinterpret_cast<const TCHAR*>(portName.data())
> reinterpret_cast<const TCHAR*>(portName.toUShort())
>
> These all compile but don't work with the function that I call.
>
> But if I use std::string as a buffer like
>
> reinterpret_cast<const TCHAR*>(portName.toStdString().c_str())
>
> then it works.
>
> I don't understand this. Is it something to do with the consts in the
> arglist?
No.
It looks like your target function is not Unicode-safe. It expects ANSI data.
--
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/769a6a17/attachment.bin
More information about the Qt-interest-old
mailing list