ToStdWStringAndBuiltInWchar: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
'''English''' [[toStdWStringAndBuiltInWchar-SimplifiedChinese|简体中文]]
'''English''' [[toStdWStringAndBuiltInWchar SimplifiedChinese|简体中文]]<br />[[Category:HowTo]]<br />[[Category:snippets]]


=QString, std::wstring and built-in wchar_t=
= QString, std::wstring and built-in wchar_t =


==Problem statement==
== Problem statement ==


Qt advises to build your Qt based software without wchar_t as built-in type, just like the Qt libraries themselves. In some cases this is not desired by the environment or not possible because other libraries have been built with the built-in wchar_t type. This will cause obscure linker errors when using std::wstrings, and [http://doc.qt.io/qt-5.0/qtcore/qstring.html#toStdWString QString::toStdWString()] ''[qt.io]'' and [http://doc.qt.io/qt-5.0/qtcore/qstring.html#fromStdWString QString::fromStdWString()] ''[qt.io]''.
Qt advises to build your Qt based software without wchar_t as built-in type, just like the Qt libraries themselves. In some cases this is not desired by the environment or not possible because other libraries have been built with the built-in wchar_t type. This will cause obscure linker errors when using std::wstrings, and &quot;QString::toStdWString()&quot;:http://doc.qt.io/qt-5.0/qtcore/qstring.html#toStdWString and &quot;QString::fromStdWString()&quot;:http://doc.qt.io/qt-5.0/qtcore/qstring.html#fromStdWString.


==Possible solution==
== Possible solution ==


Windows uses utf-16 for its character encoding, as does Qt. Using this information we can use the following code to work around the issue:
Windows uses utf-16 for its character encoding, as does Qt. Using this information we can use the following code to work around the issue:


Note that for other platforms the std::wstring may be implemented using uint32 (for ucs-4/utf-32) instead.
<code>/*! Convert a QString to an std::wstring '''/<br />std::wstring qToStdWString(const QString &amp;str)<br />{<br />#ifdef _MSC_VER<br /> return std::wstring((const wchar_t''')str.utf16());<br />#else<br /> return str.toStdWString();<br />#endif<br />}


===Categories:===
/*! Convert an std::wstring to a QString '''/<br />QString stdWToQString(const std::wstring &amp;str)<br />{<br />#ifdef _MSC_VER<br /> return QString::fromUtf16((const ushort''')str.c_str());<br />#else<br /> return QString::fromStdWString(str);<br />#endif<br />}</code>
 
* [[:Category:HowTo|HowTo]]
* [[:Category:snippets|snippets]]

Revision as of 10:05, 24 February 2015

English 简体中文

QString, std::wstring and built-in wchar_t

Problem statement

Qt advises to build your Qt based software without wchar_t as built-in type, just like the Qt libraries themselves. In some cases this is not desired by the environment or not possible because other libraries have been built with the built-in wchar_t type. This will cause obscure linker errors when using std::wstrings, and "QString::toStdWString()":http://doc.qt.io/qt-5.0/qtcore/qstring.html#toStdWString and "QString::fromStdWString()":http://doc.qt.io/qt-5.0/qtcore/qstring.html#fromStdWString.

Possible solution

Windows uses utf-16 for its character encoding, as does Qt. Using this information we can use the following code to work around the issue:

/*! Convert a QString to an std::wstring '''/<br />std::wstring qToStdWString(const QString &amp;str)<br />{<br />#ifdef _MSC_VER<br /> return std::wstring((const wchar_t''')str.utf16());<br />#else<br /> return str.toStdWString();<br />#endif<br />}

/*! Convert an std::wstring to a QString '''/<br />QString stdWToQString(const std::wstring &amp;str)<br />{<br />#ifdef _MSC_VER<br /> return QString::fromUtf16((const ushort''')str.c_str());<br />#else<br /> return QString::fromStdWString(str);<br />#endif<br />}