string 用于国际化的 Qt tr 在 main 函数中不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2020013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Qt tr for internationalisation does not work in main function?
提问by Passionate programmer
Qt's translation function tr
does not work in the main
function but works fine in a QWidget member function. Why is that?
Qt 的翻译函数tr
在函数中不起作用,main
但在 QWidget 成员函数中工作正常。这是为什么?
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QDialog dialog;
QString temp = tr("dadasda");//error:tr was not declared in this scope
dialog.show();
return a.exec();
}
回答by Passionate programmer
The translation function tr
is a static method of QObject
. Since QWidget
is a subclass of QObject
, tr
is available in methods of QWidget
, but in main()
you have to use QObject::tr
in order to use the function, as shown below.
翻译函数tr
是 的静态方法QObject
。由于QWidget
是 的子类QObject
,tr
在 的方法中可用QWidget
,但在main()
您必须使用QObject::tr
才能使用该函数,如下所示。
#include <QObject>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QDialog dialog;
QString temp = QObject::tr("dadasda");//works fine
dialog.show();
return a.exec();
}