TCHAR to QString


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 FoersterProgrammerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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));
  qs = QString::fromStdWString(std::string(p));

  return qs;

// ...

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

Open in new window

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


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

Ingo FoersterProgrammerAuthor Commented:
I use now: QString::fromWCharArray(chError)
works well.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

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.

Ingo FoersterProgrammerAuthor Commented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.