如何使用QString指定unicode字符?

lau*_*ent 23 unicode qstring qt

如何使用QString通过代码(例如"4FF0")指定unicode字符?我试过QString s("\u4FF0");但它只输出一个问号.知道怎么做吗?

编辑:

它的工作原理是这样,但是有更直接的方法吗?

std::wstring str = L"\u4FF07";
QString s = QString::fromStdWString(str));
Run Code Online (Sandbox Code Playgroud)

Ste*_*Chu 25

如果直接你的意思是使用Unicode代码点值,那么QChar可能是:

QString s = QChar(0x4FF0);
Run Code Online (Sandbox Code Playgroud)


ale*_*sdm 16

显然'\ u'仅适用于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");
Run Code Online (Sandbox Code Playgroud)


小智 5

从 C++11 开始,您可以使用 UTF-8 字符串字面量前缀 ( u8):

QString s(u8"\u4FF0");
Run Code Online (Sandbox Code Playgroud)