QString convert to LPCWSTR ?
-
how QString convert to LPCWSTR ?
-
QString foo("Hello world"); auto bar = std::make_unique<wchar_t[]>(foo.length() + 1); foo.toWCharArray(bar.get()); //toWCharArray does not append null terminator bar[foo.length()] = 0; //so add it manually MessageBox(NULL, bar.get(), L"Some caption", MB_OK);
or shorter:
QString foo("Hello world"); MessageBox(NULL, foo.toStdWString().c_str(), L"Some caption", MB_OK);
but be aware that the pointer returned in the second example is temporary, so the above is ok, but this is wrong:
QString foo("Hello world"); LPCWSTR bar = foo.toStdWString().c_str(); MessageBox(NULL, bar, L"Some caption", MB_OK); //bar is invalid at this point
-
can i use:
reinterpret_cast<LPCWSTR>(qstring.utf16()) ? -
I personally cringe whenever I see
reinterpret_cast
but yes, it would work too.