Qt tr() not working if i am using in static member
-
#1 Please let me know why it is not working i am new in Qt. here is the code
@
#include <QApplication>
#include <QPushButton>
#include <QTranslator>
class Transl
{
Q_OBJECT
public:
static const QString str;
};
const QString Transl::str = QObject::tr("Hello world");
// const char* const gstr = "Hello World";int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QTranslator translator;
translator.load("trans_la");
app.installTranslator(&translator);QPushButton *button = new QPushButton(); button->setText(Transl::str); // button->setText(tr(gstr)); // This is also throw error "error: ‘tr’ was not declared in this scope" button->resize(100,100); button->show() return app.exec();
}@
My trans_la.ts file contains this which i generate by lupdate.
@<TS version="2.0">
<context>
<name>QObject</name>
<message>
<location filename="main.cpp" line="10"/>
<source>Hello world</source>
<translation>Orbis, te saluto!</translation>
</message>
</context>
</TS> @Then I do lrelease which generate trans_la.qm. But when i run my executable instead of showing the translated string it shows "Hello world". Please suggest me why it is not working.
#2 Also if you see i have commented two lines(11th and 22nd) where I am trying to use with constant char* global variable where it thows error tr() was not declared in the scope.
-
Hi and welcome to devnet,
IIRC, for static text like that it's QT_TRANSLATE_NOOP that should be used
-
A little search in the documentation would have returned the "explanation":http://qt-project.org/doc/qt-4.8/linguist-programmers.html#using-qt-tr-noop-and-qt-translate-noop
-
It simple: tr is a method of QObject and main is a function not a QObject
-