Solve the problem of Chinese garbled characters in Qt

Solve the problem of Chinese garbled characters in Qt

In Qt, the Chinese titles generally set in the designer interface can be displayed normally.
But in QStringChina, for example, when QPainterdrawing Text, if the incoming char* contains Chinese characters, it will generally display garbled characters. At this time, two places need to be set:

1. Set QTextCodec in main.cpp
2. Use the static method fromLocal8Bit(char*) in QString

For the first place, it can be set as follows in main.cpp:

#include "MainWindow.h"

#include <QApplication>
#include <QTextCodec>

int main(int argc, char *argv[])
{
    
    
    QApplication a(argc, argv);
    QTextCodec* codec = QTextCodec::codecForName("GB2312");
    QTextCodec::setCodecForLocale(codec);
    MainWindow w;
    w.show();
    return a.exec();
}

For the second place, use the following method in char* containing Chinese:

QString::fromLocal8Bit(char*)

Where QString needs to be passed in, if it contains Chinese, the settings in the above two places need to be made.

Original link: Solving the problem of Chinese garbled characters in Qt

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_40933653/article/details/133501034