简体   繁体   English

Qt 不支持 Unicode for Visual Studio 或字体错误

[英]Qt dont support Unicode for Visual studio or Font error

im beginer, and now im learning Qt and work with Visual Studio, Extension Qt VS Tool 2.4.3, I use Text is Vietnamese language and when i build program its good but button show special characters.我是初学者,现在我正在学习 Qt 并使用 Visual Studio,扩展 Qt VS Tool 2.4.3,我使用文本是越南语,当我构建程序时它很好,但按钮显示特殊字符。

I have read a few topic about font errors but its not related with this error.我已经阅读了一些关于字体错误的主题,但它与此错误无关。

图像问题错误

My code:我的代码:

#include <QtWidgets/QApplication>
#include <QPushButton>
#include <QLabel>

int main(int argc, char *argv[])
{
        QApplication app(argc, argv);
        QPushButton nutBam; // instance 
        nutBam.setText("Nút khẩn cấp !"); //set Text button
        nutBam.show();
        return app.exec();
}

Two issues:两个问题:

  1. It's usually not a good idea to have hardcoded unicode characters in a source file.在源文件中使用硬编码的 unicode 字符通常不是一个好主意。 Editors and compilers can sometimes do the right thing.编辑器和编译器有时可以做正确的事情。 Editors can save as UTF-8 or Unicode.编辑器可以另存为 UTF-8 或 Unicode。 Compilers can assume read BOM headers and switch encoding... usually.编译器可以假设读取 BOM 标头并切换编码......通常。 But team members and source code repository systems often do the wrong thing.但是团队成员和源代码存储库系统经常做错事。 Someone else comes along with a different editor, source control system, etc... and the unicode stuff gets messed up.其他人带来了不同的编辑器、源代码控制系统等……而 unicode 的东西就搞砸了。 It also breaks diffing tools as well.它还破坏了不同的工具。 I've seen it happen way too many times.我已经看到它发生太多次了。

  2. Qt's QString sees you are passing an 8-bit ascii string into the QString constructor. Qt 的 QString 看到您将 8 位 ascii 字符串传递到 QString 构造函数中。 Then their encoding interpretation rules kick in. So it will do better with a unicode string.然后他们的编码解释规则开始生效。所以它会用 unicode 字符串做得更好。

To get the best of both worlds, store your string in source code with unicode escape characters, but pass to QString constructor as a wide string:为了两全其美,请将您的字符串存储在带有 unicode 转义字符的源代码中,但作为宽字符串传递给 QString 构造函数:

I used this tool online to convert some of your characters to \ꯍ escape sequences.在线使用此工具将您的某些字符转换为\ꯍ转义序列。

Instead of this:取而代之的是:

nutBam.setText("Nút khẩn cấp !");

This:这个:

const wchar_t* text = L"N\u00fat kh\u1ea9n c\u1ea5p !";
QString qText(text);
nutBam.setText(qText);

Although it's no longer an issue, make sure your editor is saving the source as ANSI or UTF-8 and not 16-bit unicode.尽管这不再是问题,但请确保您的编辑器将源文件保存为 ANSI 或 UTF-8 而不是 16 位 unicode。

I don't have Qt installed locally at the moment, but the above might consolidate down to just this:我目前没有在本地安装 Qt,但上述内容可能会合并为:

nutBam.setText(L"N\u00fat kh\u1ea9n c\u1ea5p !");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM