Avatar of Ingo Foerster
Ingo Foerster

asked on 

TCHAR to QString

Hello,

I need to convert a TCHAR to QString. He solutions I found do not work. I have:

TCHAR chError[2048];
I tried
 QString strError = QString::fromUtf16(chError);

But compiler said:  Fehler: C2664: 'QString::fromUtf16' : cannot convert parameter 1 from 'TCHAR [2048]' to 'const ushort *'

Any idea?

Ingo
C++

Avatar of undefined
Last Comment
Ingo Foerster
Avatar of jkr
jkr
Flag of Germany image

Again, that will depend on whether your project is built as ANSI or UNICODE. I'd suggest a helper wrapper similar to the one in your last wuestion, e.g.

QString QStringFromTCHAR(const TCHAR* p) {

  QString qs;

#ifdef _UNICODE
  qs = QString::fromStdWString(std::string(p));
#else
  qs = QString::fromStdWString(std::string(p));
#endif

  return qs;
}

// ...

TCHAR chError[2048];
 QString strError = QStringFromTCHAR(chError);

Open in new window

Avatar of sarabande
sarabande
Flag of Luxembourg image

But compiler said:  Fehler: C2664: 'QString::fromUtf16' : cannot convert parameter 1 from 'TCHAR [2048]' to 'const ushort *'

if UNICODE macro is not defined the TCHAR[2048] is char[2048] and the conversion would work if you simply initialize the QString with the char string cause QString has a constructor that takes a const char *:

QString strError = chError;

Open in new window



but if UNICODE is defined the initialization fails since QString accepts  'unsigned short*' while wchar_t is 'signed' and an own type, which means it can only converted to short or unsigned short by doing an explicit cast though both wchar_t and short are 16-bit integers.

that is all very bad and the conversion could only be done by

QString strError = (const ushort*)chError;

Open in new window


or

QString strError = reinterpret_cast<const ushort*>chError;

Open in new window


what both is not very convenient.

jkr showed how you could use std::string and std::wstring as a helper (in the second block there is a typo and std::wstring was meant) but actually this is also not very elegant especially if it still is based on the TCHAR wich you can't use anyhow (see your other question).

so i would recommend to using class QChar and QString wherever it is possible and only convert to wchar_t or wstring when there is no alternative. a normal c cast as with

QString qs = "somewhat";
...
std::wstring ws = (const wchar_t*)qs.utf16();

probably is the easiest and most transparent way.

or you define helpers W2US, CW2CUS and US2W, CUS2CW which do the job

unsigned short * W2US(wchar_t* ws) { return (unsigned short*)ws); }
const unsigned short * CW2CUS(const wchar_t* ws) { return (const unsigned short*)ws); }
wchar_t * US2W(unsigned short* us) { return (wchar_t*)us); }
const wchar_t CW2CUS(const unsigned short* us) { return (const wchar_t*)us); }

Open in new window


Sara
ASKER CERTIFIED SOLUTION
Avatar of Ingo Foerster
Ingo Foerster

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of sarabande
sarabande
Flag of Luxembourg image

good.

since you couldn't do the T-switch from UTF16 to char with that code, i would recommend not using the T types like TCHAR, LPCTSTR but using the c++ equivalents wchar_t, const wchar_t *, ... at least for new code. you also could use WCHAR, LPCWSTR, ... what is equivalent but again proprietary to MS and therefore less recommendable.

Sara
Avatar of Ingo Foerster
Ingo Foerster

ASKER

I develop on MAC so I cannot use MS formats.  It seems the library I used was developed on MS so I always have to convert in many places.
C++
C++

C++ is an intermediate-level general-purpose programming language, not to be confused with C or C#. It was developed as a set of extensions to the C programming language to improve type-safety and add support for automatic resource management, object-orientation, generic programming, and exception handling, among other features.

58K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo