【原创】QT中文化例子程序

前言:
   本文使用的环境如下:操作系统:WIN7,QT library :4.8.1,IDE :VS2010。

例子:
   相关注释说明已经在代码里有体现,直接上代码:

 

#include <Qpushbutton>
#include <QApplication>
#include <Qwidget>
#include <QTextCodec>
#include <QObject>
int main(int argc, char *argv[])
{
 QApplication app(argc, argv);

 /*如果出现是小方块,而不是乱码,有可能是没有字库*/
 /*可更换字体解决*/
 QFont font("Times",12,QFont::Normal,FALSE);
 app.setFont(font);

 QWidget windows;
 windows.setMinimumSize(300,200);
 windows.setMaximumSize(300,200);

 /*方法一:设置QObject的成员函数tr()的编码*/
 QTextCodec *textc=QTextCodec::codecForName("gbk");
 QTextCodec::setCodecForTr(textc); 
 QPushButton QPushButton_test1(QObject::tr("测试代码1"),&windows);
 QPushButton_test1.setGeometry(20,20,100,30);


 /*方法二:使用QString的fromLocal8Bit()函数*/
 QString str1;
 str1 = str1.fromLocal8Bit("测试代码2");
 QPushButton QPushButton_test2(str1,&windows);
 QPushButton_test2.setGeometry(20,60,100,30);

 /*方法三:用QTextCodec的toUnicode方法来显示中文*/
 QTextCodec *codec = QTextCodec::codecForLocale();
 QString str2 = codec->toUnicode("测试代码3");

 QPushButton QPushButton_test3(str2,&windows);
 QPushButton_test3.setGeometry(20,100,100,30);


 windows.show();
 return app.exec();
}

 

posted @ 2016-04-12 20:58  xiaoleisme  阅读(251)  评论(0编辑  收藏  举报