qt界面默认是utf8,Vs是gb2312,所以会有乱码,所以在输入时,将gb2312转换为utf8,用setWindowsTitle示例,直接输入中文,qt界面就会乱码,经过转换后的返回值输入就不会再乱码了
#include <qtextcodec.h>
#include <string>
构造函数
{
ui.setupUi(this);
std::string temp("模板方法");
char str[10] = {0};
setWindowTitle(TemplateMethod::Gb2312_utf8(temp, str));
}
char * TemplateMethod::Gb2312_utf8(std::string strutf8, char* str)
{
//头文件QTextCodec
QTextCodec* utf8Codec = QTextCodec::codecForName("utf-8");
QTextCodec* gb2312Codec = QTextCodec::codecForName("gb2312");
QString strUnicode = gb2312Codec->toUnicode(strutf8.c_str());
QByteArray ByteUtf8 = utf8Codec->fromUnicode(strUnicode);
strcpy(str, ByteUtf8.data());
return str;
}
char* TemplateMethod::utf8_Gb2312(std::string strgb2312, char* str)
{
QTextCodec* utf8Codec = QTextCodec::codecForName("utf-8");
QTextCodec* gb2312Codec = QTextCodec::codecForName("gb2312");
QString strUnicode = utf8Codec->toUnicode(strgb2312.c_str());
QByteArray ByteGb2312 = gb2312Codec->fromUnicode(strUnicode);
strcpy(str, ByteGb2312.data());
return str;
}