Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Qt 5.3 fails to display UTF-8 character

I am trying to display a unicode character (Euro sign) on a button using Qt and C++ in Visual Studio 2013. I tried the following code:

_rotateLeftButton->setText("\u20AC");

and

_rotateLeftButton->setText("€");

and

_rotateLeftButton->setText(QString::fromUtf8("\u20AC"));

and

_rotateLeftButton->setText(QString::fromUtf8("€"));

However, all of those lines result in the following:

Character can not be displayed

All my code files are UTF-8 encoded, except for the moc files (.cxx). For whichever reason the moc executable does not generate them using unicode. Yet I was not able to get this unicode symbol displayed correctly. I also tried setting another font than the default one withouth success. Does anyone know what could be the problem?

Thank you for your help.

like image 791
bweber Avatar asked Jan 03 '15 09:01

bweber


People also ask

Does C use UTF-8?

Most C string library routines still work with UTF-8, since they only scan for terminating NUL characters.

Can UTF-8 represent all characters?

Each UTF can represent any Unicode character that you need to represent. UTF-8 is based on 8-bit code units. Each character is encoded as 1 to 4 bytes. The first 128 Unicode code points are encoded as 1 byte in UTF-8.

What characters are not in UTF-8?

0xC0, 0xC1, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF are invalid UTF-8 code units. A UTF-8 code unit is 8 bits. If by char you mean an 8-bit byte, then the invalid UTF-8 code units would be char values that do not appear in UTF-8 encoded text.

Is UTF-8 a character set?

UTF-8 is a character encoding system. It lets you represent characters as ASCII text, while still allowing for international characters, such as Chinese characters.


1 Answers

QString::fromUtf8("€")

Will work if the file really is handled as UTF-8. As @n.m. commented, VS requires some help from a faux-BOM to ensure this.

QString::fromUtf8("\u20AC")

\u doesn't make sense in a byte string literal. You could spell it using \x byte escapes for the UTF-8 encoded version:

QString::fromUtf8("\xE2\x82\xAC")

Or use a wide string literal:

QString::fromWCharArray(L"\u20AC")
like image 97
bobince Avatar answered Oct 10 '22 05:10

bobince