简体   繁体   中英

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.

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. Editors and compilers can sometimes do the right thing. Editors can save as UTF-8 or Unicode. Compilers can assume read BOM headers and switch encoding... usually. 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. 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. Then their encoding interpretation rules kick in. So it will do better with a unicode string.

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:

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.

I don't have Qt installed locally at the moment, but the above might consolidate down to just this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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