Hi I need to convert a std::wstring
to QString
and I tried the most obvious way of
std::wstring wideString;
QString qtString = QStri
boost::filesystem::path also seems to require the project setting "treat wchar as built-in type = yes", so vahapt's solution was the only one I could get to work.
Edit your Visual Studio project settings and under C/C++ -> Language set the option Treat wchar_t was Built-in Type to No.
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
}
This even may happen if QtCore4.lib is linked correctly. Make sure that the VS option "Treat wchar_t as built in type" is turned OFF.
Add
QT += core
to you .pro
file. This results in what the other answer tell you to do. (be sure to rerun qmake)
I was seeing a similar problem but none of these solutions worked. I eventually realised I was compiling with Visual Studio 2017 but linking to Qt compiled with Visual Studio 2013. Once I selected the right version it worked fine.