转载自 https://chuanke.baidu.com/v4509752-209060-1284466.html
GBK.h
1 #ifndef _QT_GBK_H 2 #define _QT_GBK_H 3 4 5 #include <QString> 6 #include <QTextCodec> 7 #include <string> 8 using std::string; 9 10 class GBK 11 { 12 public: 13 // QString(Unicode) -> std::string (GBK) 14 static string FromUnicode(const QString& qstr) 15 { 16 QTextCodec* pCodec = QTextCodec::codecForName("gb2312"); 17 if(!pCodec) return ""; 18 19 QByteArray arr = pCodec->fromUnicode(qstr); 20 string cstr = arr.data(); 21 return cstr; 22 } 23 24 // std::string (GBK) -> QString(Unicode) 25 static QString ToUnicode(const string& cstr) 26 { 27 QTextCodec* pCodec = QTextCodec::codecForName("gb2312"); 28 if(!pCodec) return ""; 29 30 QString qstr = pCodec->toUnicode(cstr.c_str(), cstr.length()); 31 return qstr; 32 } 33 34 }; 35 36 37 #endif