[Qt-interest] QString to TCHAR* and back
Duane Hebert
spoo at flarn.com
Wed Feb 24 21:26:48 CET 2010
>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?
More information about the Qt-interest-old
mailing list