Important: Please read the Qt Code of Conduct -

QT_TR_NOOP does not get recognized from lupdate

  • I have this code:

    #include <QApplication>
    #include <QMessageBox>
    #include <QTranslator>
    int main(int argc, char **argv)
        QApplication app{argc, argv};
        QTranslator translator;
        QVector<QString>texts = {
            QT_TRANSLATE_NOOP("main", "URL"),
            QT_TRANSLATE_NOOP("main", "Title"),
            QT_TRANSLATE_NOOP("main", "Publisher")
        QVector<QString>texts2 = {
            QT_TR_NOOP("This is a very special string."),
            QT_TR_NOOP("And this is just as special")};
        QMessageBox::information( nullptr,
             qApp->translate(nullptr,texts2[1].toStdString().c_str()) );

    If I run lupdate only the strings in QT_TRANSLATE_NOOP get added to the translation file. WHY QT_TR_NOOP is not recognized?

    It gives tr() cannot be called without context for the lines with QT_TR_NOOP

    Also it says in the help that qApp->translate() is obsolete bur does not mention whats the alternative in QT5.

  • Qt Champions 2019

    For QT_TR_NOOP you need a context (= class derived from QObject) where you define your strings to translate. So since you don't have a class here the correct way is to use QT_TRANSLATE_NOOP
    And why do you store your untranslated strings as QString at all? Simple char* would be enough and would save you the strange toStdString().c_str() stuff (which should be replaced to toUtf8().constData() if really needed...)

  • well i have it from an example in QT4 which looks like this:

    #include <QApplication>
    #include <QMessageBox>
    #include <QTranslator>
    int main( int argc, char **argv )
      QApplication app( argc, argv );
      QTranslator translator;
      translator.load( "noop_sv_SE" );
      app.installTranslator( &translator );
      char *texts[] = { QT_TRANSLATE_NOOP("main","URL"),
                        QT_TRANSLATE_NOOP("main","Publisher") };
      char *texts2[] = { QT_TR_NOOP( "This is a very special string."),
                         QT_TR_NOOP( "And this is just as special.") };
      QMessageBox::information( 0, qApp->translate("main",texts[2]), qApp->translate(0,texts2[1]) );
      return 0;

    I turn on c++17 compilation in the project. Than i get complains that in C++11 to convert from char* to string literals is not allowed. Also in this example code QT_TR_NOOP is used and seems to work. But its based on a old QT4 version.

  • Qt Champions 2019

    Because your char strings must be const -> const char *texts[].
    QT_TR_NOOP can't work - see documentation.

Log in to reply