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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:36:24  来源:igfitidea点击:

Qt tr for internationalisation does not work in main function?

stringqtinternationalization

提问by Passionate programmer

Qt's translation function trdoes not work in the mainfunction 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 tris a static method of QObject. Since QWidgetis a subclass of QObject, tris available in methods of QWidget, but in main()you have to use QObject::trin order to use the function, as shown below.

翻译函数tr是 的静态方法QObject。由于QWidget是 的子类QObjecttr在 的方法中可用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();
}