Qt tr doesn't seem to work on static constant members?

I am working on translating our Qt gui at the moment.

I have the following code:

// header file
static const QString Foo;

// cpp file
const QString FooConstants::Foo = "foo"; 

// another cpp file
editMenu->addAction(tr(FooConstants::Foo));

This does not seem to work.

That is, the specified constant is missing in the .ts file.

If I do this, it works:

// another cpp file
editMenu->addAction(tr("foo"));

However, this constant is used in many places, and I do not want to manually update each string literal. (if this should change in the future)

Can anyone help?

+4
source share
3 answers

Wrap your literal in a macro : QT_TR_NOOP

// cpp file
const QString FooConstants::Foo = QT_TR_NOOP("foo");

From the manual :

, : QT_TR_NOOP() QT_TRANSLATE_NOOP(). lupdate. ( ).

+9

, .

, Qt , , . - tr("text"), , QT_TRANSLATE_NOOP QT_TR_NOOP, .

+2
editMenu->addAction(tr(FooConstants::Foo));

, , tr char *, QString:

QString QObject::tr ( const char * sourceText, const char * disambiguation = 0, int n = -1 )

FooConstants:: Foo char * , :

 const QByteArray byteArray = FooConstants::Foo.toLatin1();
 char *data = byteArray.data();
 editMenu->addAction(tr(data));
0
source

Source: https://habr.com/ru/post/1718696/


All Articles