QString convert to LPCWSTR ?
General and Desktop
4
Posts
2
Posters
3.5k
Views
2
Watching
-
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
-
I personally cringe whenever I see
reinterpret_cast
but yes, it would work too.