// 封装 #ifndef _QT_GBK_H #define _QT_GBK_H #include<QString> #include<QTextCodec> #include<string> using std::string; class GBK { public: static string FromUnicode(const QString& qstr) { QTextCodec* pCodec = QTextCodec::codecForName("gb2312"); if (!pCodec)return ""; QByteArray arr = pCodec->fromUnicode(qstr); string cstr=arr.data(); return cstr; } static QString ToUnicode(const string& cstr) //QBT转换QString { QTextCodec* pCodec = QTextCodec::codecForName("gb2312"); if (!pCodec)return ""; QString qstr = pCodec->toUnicode(cstr.c_str(), cstr.length()); return qstr; } }; #endif// 使用:: QString str = GBK::ToUnicode("你好中国"); ui.linetxt->setText(str); //uilinetxt->setText(GBK::ToUnicode("你好中国")) string text = GBK::FromUnicode(ui.linetxt->text());//获取用户输入
时间: 2024-10-12 07:25:29