How to specify a unicode character using QString?

允我心安 提交于 2019-11-29 16:24:00

问题


How can I specify a unicode character by code (such as "4FF0") using QString? I tried QString s("\u4FF0"); but it only outputs a question mark. Any idea how to do this?

Edit:

It works that way, but is there a more direct way?

std::wstring str = L"\u4FF07";
QString s = QString::fromStdWString(str));

回答1:


If by direct you mean using a Unicode code point value, then QChar may be it:

QString s = QChar(0x4FF0);



回答2:


Apparently '\u' only works with UTF-8:

QString s = QString::fromUtf8("\u4FF0");

// Or with that at the start of your main function:
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("utf8"));
...
QString s("\u4FF0");



回答3:


As a direct pointer, try QString(QChar(0x4FF0));

You need to ensure you have the correct utf-16 encoding.



来源:https://stackoverflow.com/questions/7586860/how-to-specify-a-unicode-character-using-qstring

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!