Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Qt tr() not working if i am using in static member
Forum Updated to NodeBB v4.3 + New Features

Qt tr() not working if i am using in static member

Scheduled Pinned Locked Moved QML and Qt Quick
9 Posts 3 Posters 18.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sysrules
    wrote on 27 Oct 2013, 06:20 last edited by
    #1

    #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&#40;&#41;;
    

    }@

    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.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dasun
      wrote on 27 Oct 2013, 09:02 last edited by
      #2

      Qt tr does not work on static constant members. use QT_TR_NOOP instead tr.

      @const QString Transl::str = QT_TR_NOOP("Hello world");@

      1 Reply Last reply
      2
      • S Offline
        S Offline
        sysrules
        wrote on 27 Oct 2013, 09:37 last edited by
        #3

        hi @dasun i tried QT_TR_NOOP("Hello world") in that case lupdate is not working it ask tr is not working without any context. Please help :). Also can you figure out about the error of #2 why tr() is giving me error ?

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dasun
          wrote on 27 Oct 2013, 16:03 last edited by
          #4

          Main function is not an QObject. tr defined in QObject.

          You can use @QObject::tr("Hello World");@

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 27 Oct 2013, 20:16 last edited by
            #5

            Hi and welcome to devnet,

            IIRC, for static text like that it's QT_TRANSLATE_NOOP that should be used

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • S Offline
              S Offline
              sysrules
              wrote on 28 Oct 2013, 15:57 last edited by
              #6

              Hi @SGalst what actually QT_TRANSLATE_NOOP will do ? will it work on translation ?

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 28 Oct 2013, 20:38 last edited by
                #7

                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

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  sysrules
                  wrote on 29 Oct 2013, 16:16 last edited by
                  #8

                  hey thanks for the valuable info. but i still did not find the solution of #2 why it is giving me error. well i'll try to look more in documentation. Thanks

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 29 Oct 2013, 21:02 last edited by
                    #9

                    It simple: tr is a method of QObject and main is a function not a QObject

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • N ngocanhnu referenced this topic on 29 Mar 2024, 08:15

                    4/9

                    27 Oct 2013, 16:03

                    • Login

                    • Login or register to search.
                    4 out of 9
                    • First post
                      4/9
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved