in C++ by (9.4k points) edited by
0 like 0 dislike
17 views

How I can specify a unicode character by code (such as "4FF0") using QString datatype in Qt framework for C++ programming language? Any idea how to do this?

I tried QString s("\u4FF0");, but it only outputs a question mark.

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

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

How to get Unicode symbol for QString?


qstring unicode symbol
qt string unicode char

1 Answer

0 like 0 dislike
by (9.4k points) edited by

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

QString s = QChar(0x4FF0);

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");

As a direct pointer, try QString(QChar(0x4FF0));
You need to ensure you have the correct UTF-16 encoding.

Your answer

Try to answer the question as detailed as possible.
Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.