View Full Version : QString to TCHAR or TCHAR*

29th May 2013, 11:51
Hi all,
I have been working since hours to figure out a way to convert a QString value to TCHAR / TCHAR*
have tried many forms and types..still no joy.
please can i get a code example to convert a QString value to TCHAR / TCHAR*

QString Value to TCHAR tValue
and QString Value to TCHAR* rValue

Thanks a lot.

29th May 2013, 19:02
You are asking in the wrong forum, you'll need to check with a Microsoft forum on how to create strings of their types from standard types like char* or std::string


29th May 2013, 22:32
In builds where UNICODE is defined (generally the case) TCHAR (http://msdn.microsoft.com/en-us/library/office/cc842072.aspx) is a typedef for/equivalent to WCHAR (http://msdn.microsoft.com/en-us/library/windows/desktop/gg269344(v=exchg.10).aspx) a single 16bit Unicode code point. It is clearly not possible to put a whole QString into a single Unicode character. If you have a buffer of TCHARs, that you address through a pointer, then QString::toUtf16() gives you characters you can copy into that buffer.

In builds where UNICODE is not set TCHAR is a typedef for char. It is clearly not possible to put a whole QString into a single char. In these cases any of QString::toUtf8(), toAscii(), toLatin1(), toLocal8bit() will give you something that may make sense in a buffer of TCHARs.

30th May 2013, 08:45
Hello ChrisW67,
QT uses UTF-8 encoding.
And i have googled..issue is not from QString to char*
QString to TCHAR which is a (WCHAR in unicode)

can i get some code help??
QString myString to TCHAR wString ??

30th May 2013, 10:44
Did you actually try anything?

const QString name("COMPUTERNAME");

// Via a std::wstring
WCHAR result1[128];
std::wstring s = name.toStdWString();
DWORD len = GetEnvironmentVariable(s.c_str(), result1, 128);
qDebug() << QString::fromWCharArray(result1);

// Via a buffer
WCHAR result2[128];
wchar_t buf[name.length() + 1];
int l = name.toWCharArray(buf);
buf[l] = 0;
len = GetEnvironmentVariable(buf, result2, 128);
qDebug() << QString::fromWCharArray(result2);

// Using utf16()
WCHAR result3[128];
len = GetEnvironmentVariable(reinterpret_cast<LPCTSTR>(name.utf16()), result3, 128);
qDebug() << QString::fromWCharArray(result3);

30th May 2013, 13:38
QT uses UTF-8 encoding.

No. From the Qt Documentation:
"QString stores a string of 16-bit QChars, where each QChar corresponds one Unicode 4.0 character. "