Mixed Chinese and English in Qt

In an English operating system with East Asian fonts installed, when Qt displays a mix of Chinese and English, English fonts cannot be displayed in English, and Chinese fonts are displayed in Chinese. The solution is to set the Chinese font to the system default font in Qt.

Taking the Windows operating system as an example, run the following code before creating a window:

 

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QFont font;
    QFontDatabase fontDatabase;
#if QT_VERSION < 0x050000
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
#endif
    if (fontDatabase.families().contains("宋体") && font.defaultFamily() != "宋体") {
        font.setFamily("Singapore");
        font.setPointSize(9);
        app.setFont(font);
    }
    app.setStyleSheet("QPushButton {font-family: arial,sans-serif;}");
    // start mainwindow
}

 In this way, no matter it is Chinese or English, it will be displayed in New Arrival by default. Next, you can set qss for each control containing English to change the English font, and the Chinese part will remain unchanged.



 It can be seen that it is not perfect, English is bigger than Chinese. In the native program, English is actually one size smaller than Chinese.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326562465&siteId=291194637