显示中文的小例子 qt4 (补充)

来源:互联网 发布:陶瓷产品设计软件 编辑:程序博客网 时间:2024/04/28 20:20

移植后发现一个问题,WINDOWS下能显示的中文,在板子上却显示的乱码,这需要在程序上稍微加一点东西,大多数人的GUI都是带又中文显示的吧?所以举个例子,大家可以再板子上跑跑看
#include <QApplication> 
#include <QPushButton> 
#include <QTextCodec>
int main(int argc, char *argv[]) 

QApplication app(argc, argv);
QFont f; 
f.setFamily("wenquanyi"); 
f.setPointSize(5);
QTextCodec *code=QTextCodec::codecForName("GBK"); 
QString uniStr=code->toUnicode("中文"); 
QPushButton hello(uniStr); 
hello.setFont(f);
hello.setText(uniStr);
hello.resize(100, 30); 
hello.show();

return app.exec(); 
}

 

//另一个例子

QByteArray cstr=" 遵循GPL协议"; 
 QTextCodec *codec=QTextCodec::codecForName("gbk"); 
 
 QString string=codec->toUnicode(cstr);
 
 QMessageBox::about(this,"about",string);

 

//如果要在整个程序使用中文,那么在main函数加上这句话就行了:

QTextCodec::setCodecForTr(QTextCodec::codecForName("gbk"));

当然,这句话需要头文件,包含 #include <QtGui> 即可


QTextCodec::setCodecForLocale(QTextCodec::codecForName("GBK"));
QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));

原创粉丝点击