| 在QT中可以直接QTextCodec来转换字符串的编码,这为在QT下开发中文软件带来了便利条件,不过这种方法不符合国际化/本地化的标准: char *string = "你好,世界!"; //QCString string= "你好,世界!"; QTextCodec *codec = QTextCodec::codecByName("GBK"); QString strText = codec->toUnicode(string); QLabel *label = new QLabel(strText);
最直接的方法是把整个应用程序的编码设置为GBK编码,然后在字符串这前加tr: qApp->setDefaultCodec( QTextCodec::codecForName("GBK") ); ... QLabel *label = new QLabel(tr("你好,世界!")); 例: #include #include
int main( int argc, char *argv[] ) { QApplication app( argc, argv ); app.setDefaultCodec( QTextCodec::codecForName("GBK") ); QLabel label(tr("你好,世界!"), NULL); app.setMainWidget(&label); label.show(); return app.exec(); }
如果使QT根据Locale的环境变量取得字符集,可以使用如下命令: QString::fromLocal8Bit("你好,世界!"); 例: #include #include
int main( int argc, char *argv[] ) { QApplication app( argc, argv ); QLabel label(QString::fromLocal8Bit("你好,世界!"), NULL); app.setMainWidget(&label); label.show(); return app.exec(); }
以上方法开发不符合国际化编程的习惯,如果编写支持多种编码的软件,一定要采用国际化方法。 |
| |