Difference between revisions of "ToStdWStringAndBuiltInWchar"

From Qt Wiki
Jump to: navigation, search
(Decode HTML entity names)
(Sub-categorize)
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
 
 
'''English''' [[toStdWStringAndBuiltInWchar SimplifiedChinese|简体中文]]
 
 
[[Category:HowTo]]
 
[[Category:HowTo]]
[[Category:snippets]]
+
[[Category:Snippets::Misc]]
  
 
= QString, std::wstring and built-in wchar_t =
 
= QString, std::wstring and built-in wchar_t =
Line 9: Line 6:
 
== 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()] and [http://doc.qt.io/qt-5.0/qtcore/qstring.html#fromStdWString QString::fromStdWString()].
+
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/qstring.html#toStdWString QString::toStdWString()] and [http://doc.qt.io/qt-5/qstring.html#fromStdWString QString::fromStdWString()].
  
 
== Possible solution ==
 
== Possible solution ==
Line 15: Line 12:
 
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:
  
<code>/*! Convert a QString to an std::wstring */
+
'''Convert a QString to an std::wstring'''
 +
<code>
 
std::wstring qToStdWString(const QString &str)
 
std::wstring qToStdWString(const QString &str)
 
{
 
{
 
#ifdef _MSC_VER
 
#ifdef _MSC_VER
  return std::wstring((const wchar_t''')str.utf16());
+
  return std::wstring((const wchar_t*)str.utf16());
 
#else
 
#else
 
  return str.toStdWString();
 
  return str.toStdWString();
 
#endif
 
#endif
 
}
 
}
 +
</code>
  
/*! Convert an std::wstring to a QString */
+
'''Convert an std::wstring to a QString'''
 +
<code>
 
QString stdWToQString(const std::wstring &str)
 
QString stdWToQString(const std::wstring &str)
 
{
 
{
 
#ifdef _MSC_VER
 
#ifdef _MSC_VER
  return QString::fromUtf16((const ushort''')str.c_str());
+
  return QString::fromUtf16((const ushort*)str.c_str());
 
#else
 
#else
 
  return QString::fromStdWString(str);
 
  return QString::fromStdWString(str);
 
#endif
 
#endif
}</code>
+
}
 +
</code>

Latest revision as of 12:27, 28 November 2016


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() and QString::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 std::wstring qToStdWString(const QString &str) {

  1. ifdef _MSC_VER
return std::wstring((const wchar_t*)str.utf16());
  1. else
return str.toStdWString();
  1. endif

}

Convert an std::wstring to a QString QString stdWToQString(const std::wstring &str) {

  1. ifdef _MSC_VER
return QString::fromUtf16((const ushort*)str.c_str());
  1. else
return QString::fromStdWString(str);
  1. endif

}