Hi I need to convert a std::wstring
to QString
and I tried the most obvious way of
std::wstring wideString;
QString qtString = QStri
Best solution to the problem is to set the option "Treat wchar_t was Built-in Type" to No. However in some cases this might not be possible.
For example xerces_c is compiled with wchar_t as Built-in Type. If you need to use both xerces_c then you must either recompile QT or xerces_c to match a common Built-in Type setting.
Windows uses UTF16 charset so does QT for unicode strings. Thus, the alternative solution below might be a life saver.
/*! Convert a QString to an std::wstring */
std::wstring qToStdWString(const QString &str)
{
#ifdef _MSC_VER
return std::wstring((const wchar_t *)str.utf16());
#else
return str.toStdWString();
#endif
}
/*! Convert an std::wstring to a QString */
QString stdWToQString(const std::wstring &str)
{
#ifdef _MSC_VER
return QString::fromUtf16((const ushort *)str.c_str());
#else
return QString::fromStdWString(str);
#endif
}