Qt utf-8编码问题
Qt utf-8 encoding issue
#include <iostream>
#include <QString>
using namespace std;
int main()
{
QString qstr = QString::fromUtf8(" ");
cout << qstr.length() << std::endl;
for (QChar c : qstr) cout << QString(c).toStdString() << endl;
}
得到输出:
19
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
预期输出:
11
Qt 版本:
Package: qt5-default Architecture: amd64 Version:
5.5.1+dfsg-16ubuntu7.7 Multi-Arch: same Priority: optional Section: universe/libs Source: qtbase-opensource-src Origin: Ubuntu
如何解决这个问题?
QString stores a string of 16-bit QChars
https://doc.qt.io/qt-5/qstring.html
" "
字符串有 4 个字节的字符。
可以使用以下代码:
QString str = QString::fromUtf8(" ");
cout << "uint 32b: " << str.toUcs4().size() << endl;
foreach (const uint & ucsChar, str.toUcs4())
cout << QChar::decomposition(ucsChar).toStdString() << endl;
#include <iostream>
#include <QString>
using namespace std;
int main()
{
QString qstr = QString::fromUtf8(" ");
cout << qstr.length() << std::endl;
for (QChar c : qstr) cout << QString(c).toStdString() << endl;
}
得到输出:
19
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
预期输出:
11
Qt 版本:
Package: qt5-default Architecture: amd64 Version: 5.5.1+dfsg-16ubuntu7.7 Multi-Arch: same Priority: optional Section: universe/libs Source: qtbase-opensource-src Origin: Ubuntu
如何解决这个问题?
QString stores a string of 16-bit QChars https://doc.qt.io/qt-5/qstring.html
" " 字符串有 4 个字节的字符。 可以使用以下代码:
QString str = QString::fromUtf8(" ");
cout << "uint 32b: " << str.toUcs4().size() << endl;
foreach (const uint & ucsChar, str.toUcs4())
cout << QChar::decomposition(ucsChar).toStdString() << endl;