继续Qt中文问题
昨天关于中文编码的问题,其实还有一点小小的错误,今天先用一小段代码解释下所说的错误在哪里。
#include <QApplication>
#include <QLabel>
#include <QTextCodec>
int main(int argc, char* argv[])
{
QApplication app(argc,argv);
QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
QLabel *label = new QLabel(tr("这里是中文"));
label->Show();
return app.exec();
}
编译代码,得到的错误是: 'tr'在此作用域中尚未声明。
昨天为什么没有出现这种错误呢?因为昨天的代码是从qt creator生成的MainWindow中挑出来的,tr被声明为QObject的一个static方法,因此在MainWindow中使用tr不会有问题。
把上面的
QLabel *label=new QLabel(tr("这里是中文"));
改为
QLabel *label=new QLabel(QObject::tr("这里是中文"));
这下就OK了。
#include <QApplication>
#include <QLabel>
#include <QTextCodec>
int main(int argc, char* argv[])
{
QApplication app(argc,argv);
QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
QLabel *label = new QLabel(tr("这里是中文"));
label->Show();
return app.exec();
}
编译代码,得到的错误是: 'tr'在此作用域中尚未声明。
昨天为什么没有出现这种错误呢?因为昨天的代码是从qt creator生成的MainWindow中挑出来的,tr被声明为QObject的一个static方法,因此在MainWindow中使用tr不会有问题。
把上面的
QLabel *label=new QLabel(tr("这里是中文"));
改为
QLabel *label=new QLabel(QObject::tr("这里是中文"));
这下就OK了。