问题描述
我试图在Visual Studio 2013中使用Qt和C ++在按钮上显示unicode字符(欧元符号)。我尝试下面的代码:
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");
和
_rotateLeftButton->setText("€");
和
_rotateLeftButton->setText(QString::fromUtf8("\u20AC"));
和
_rotateLeftButton->setText(QString::fromUtf8("€"));
但是,所有这些行都会导致以下结果:
However, all of those lines result in the following:
我的所有代码文件都是UTF-8编码的,除了moc文件(.cxx)。对于任何原因,moc可执行文件不使用unicode生成它们。然而我不能得到这个unicode符号显示正确。我也尝试设置另一种字体比默认的成功。有没有人知道可能是什么问题?
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?
感谢您的帮助。
推荐答案
QString::fromUtf8("€")
如果文件真的被作为UTF-8处理,将工作。 As @ n.m。评论,VS需要从人工BOM的一些帮助,以确保这一点。
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
在字节字符串字面量。您可以使用UTF-8编码版本的 \x
字节转义来拼写:
\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")
或使用宽字符串:
QString::fromWCharArray(L"\u20AC")
这篇关于C ++:Qt 5.3无法显示UTF-8字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!