Qstring to LPCWSTR

I had the same issue (I'm using Qt 5.3), this is how I fixed it:

QString strVariable1;
LPCWSTR strVariable2 = (const wchar_t*) strVariable1.utf16();

QString::utf16() returns const ushort*, which is different from const wchar_t*, so you have the compile error.

You are probably compiling with /Zc:wchar_t. If you change it to /Zc:wchar_t- it should work, as wchar_t type becomes typedef to 16-bit integer in this case.

In Visual Studio: Project Properties->Configuration Properties->C/C++->Treat WChar_t As Built in Type->No.

Or just add reinterpret_cast<LPCWSTR>.

Tags:

Qt