Qt和unicode转义字符串

Uns*_*tal 5 unicode qt escaping utf-8

我是通过信号和插槽获取服务器数据的.这是插槽部分:

QString text(this->reply->readAll());
Run Code Online (Sandbox Code Playgroud)

Problem is, that in text variable will be unicode escape, for example:

\u043d\u0435 \u043f\u0430\u0440\u044c\u0441\u044f ;-)
Run Code Online (Sandbox Code Playgroud)

Is there any way to convert this?

McK*_*McK 7

我想这就是你需要的:

(使用正则表达式查找出现的\ uCCCC,并用基数为16的unicode号CCCC的QChar替换它们)

QRegExp rx("(\\\\u[0-9a-fA-F]{4})");
int pos = 0;
while ((pos = rx.indexIn(str, pos)) != -1) {
    str.replace(pos++, 6, QChar(rx.cap(1).right(4).toUShort(0, 16)));
}
Run Code Online (Sandbox Code Playgroud)


Ada*_*m W 2

你试过了吗:

QString text = QString::fromUtf8(this->reply->readAll());
Run Code Online (Sandbox Code Playgroud)

http://doc.qt.io/qt-5/qstring.html#fromUtf8

假设是Utf8,否则使用fromUtf16