Given the encoding of the project is probably Unicode (but not for sure) what is the best way of converting ATL::CString to QString?
What I have thought of is this:
CString c(_T("SOME_TEXT"));
//...
std::basic_string<TCHAR> intermediate((LPCTSTR)c);
QString q;
#ifdef _UNICODE
q = QString::fromStdWString(intermediate);
#else
q = QString::fromStdString(intermediate);
#endif
Do you think that it works? Any other ideas?
You don’t need the intermediate conversion to a
std::string. TheCStringclass can be treated as a simple C-style string; that is, an array of characters. All you have to do is cast it to anLPCTSTR.And once you have that, you just need to create the
QStringobject depending on whether the characters in yourCStringare of typecharorwchar_t. For the former, you can use one of the standard constructors forQString, and for the latter, you can use thefromWCharArrayfunction.Something like the following code (untested, I don’t have Qt installed anymore):
Edit: As suggested in the comments, you have to disable “Treat
wchar_tas a built-in type` in your project’s Properties to get the above code to link correctly in Visual Studio (source).For
_UNICODE, I believe you could also use thefromUtf16function: