ToStdWStringAndBuiltInWchar

From Qt Wiki
Revision as of 17:58, 14 January 2015 by Maintenance script (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

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() [qt.io] and QString::fromStdWString() [qt.io].

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:

Note that for other platforms the std::wstring may be implemented using uint32 (for ucs-4/utf-32) instead.

Categories: