Solve the problem of QString Chinese garbled in qt

Solve the problem of Chinese garbled in Qt


In Qt, the Chinese title set in the designer interface can be displayed normally.
But in QString, for example, when QPainter draws Text, if the incoming char* contains Chinese, it will generally display garbled characters. Two places need to be set at this time:

  1. Set QTextCodec in main.cpp
  2. QString uses the static method fromLocal8Bit(char*)
    for the first place, which 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, the following method is used in the char* that contains Chinese:

QString::fromLocal8Bit(char*)

In the place where QString needs to be passed in, if Chinese is included, the settings in the above two places are required.

Guess you like

Origin blog.csdn.net/GeomasterYi/article/details/110916311
Recommended