26

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));
laurent
  • 79,308
  • 64
  • 256
  • 389

4 Answers4

29

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

QString s = QChar(0x4FF0);
feedc0de
  • 3,260
  • 4
  • 26
  • 50
Stephen Chu
  • 12,280
  • 1
  • 32
  • 46
17

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");
alexisdm
  • 27,787
  • 6
  • 53
  • 84
4

Since C++11 you can use UTF-8 string literal prefix (u8):

QString s(u8"\u4FF0");
Timur
  • 41
  • 3
0

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

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