AtUtf8.h
- #ifndef _QT_UTF8_H
- #define _QT_UTF8_H
-
- #include <QString>
- #include <string>
- using std::string;
-
- class AfUtf8
- {
- public:
-
- static string ToString(const QString& qstr)
- {
- QByteArray arr = qstr.toUtf8();
- string cstr = arr.data();
- return cstr;
- }
-
-
- static QString ToQString(const string& cstr)
- {
- QString qstr = QString::fromUtf8(cstr.c_str(), cstr.length());
- return qstr;
- }
- };
- #endif
AtGbk.h
- #ifndef _QT_GBK_H
- #define _QT_GBK_H
-
- #include <QString>
- #include <QTextCodec>
- #include <string>
- using std::string;
-
- class AfGbk
- {
- public:
-
- static string ToString(const QString& qstr)
- {
- QTextCodec* pCodec = QTextCodec::codecForName("gb2312");
- if(!pCodec) return "";
-
- QByteArray arr = pCodec->fromUnicode(qstr);
- string cstr = arr.data();
- return cstr;
- }
-
-
- static QString ToQString(const string& cstr)
- {
- QTextCodec* pCodec = QTextCodec::codecForName("gb2312");
- if(!pCodec) return "";
-
- QString qstr = pCodec->toUnicode(cstr.c_str(), cstr.length());
- return qstr;
- }
-
- };
- #endif
http://blog.csdn.net/bladeandmaster88/article/details/53469959
string与QString转换(string既可以是utf8,也可以是gbk)
原文:http://www.cnblogs.com/findumars/p/6345115.html