QT Chinese display problems

(This article refer to the https://www.cnblogs.com/xgponder/p/4744168.html ).

Do QT development could easily run into the problem of Chinese garbled, then the question is how it happened, and how to solve it?

A phenomenon described first: There are two previous programs hand, a part of the code as follows:

QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));    
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));    
QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
QMessageBox::about(NULL,QObject::tr("提示"),QObject::tr("密码错误,请重新输入!"));

Another code is as follows:

QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));    
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));    
QTextCodec::setCodecForTr(QTextCodec::codecForName("gb18030"));
QMessageBox::about(NULL,QObject::tr("提示"),QObject::tr("密码错误,请重新输入!"));

The reality is that after these two procedures to implement prompt box Chinese are normal, if the first program code changed to gb18030, the second change to utf8, the two programs to implement the prompt box are garbled , which is very interesting.

After examination, a first encoding procedure is the UTF8, the second coding procedure is gb18030. You will find this code and encoding is consistent. It should be mentioned in QT QString use Unicode encoded, it should first of all to show Chinese Chinese converted to Unicode encoding. This time the question came up, what is your source coding format is it, it is to know your source coding format conversion can give you ah. So, QTextCodec :: setCodecForTr (QTextCodec :: codecForName ( "UTF-8")) This sentence is telling you to QT encoding tr surrounding text, encoding here needs to be consistent with the encoding your source code.

Also to say, the above function can only be used in QT4, currently QT5 has canceled the interface.

Published 12 original articles · won praise 4 · views 20000 +

Guess you like

Origin blog.csdn.net/wuzhidefeng/article/details/81979943