Results 1 to 3 of 3

Thread: translation loading / execution order / QObject magic(?)

  1. #1
    Join Date
    Jun 2006
    Posts
    81
    Thanks
    6
    Thanked 5 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: translation loading / execution order / QObject magic(?)

    (Sry made a mistake while editing the post. This is a reconstruction.)

    My app loads modules which themselves are responsible to load their translations. I wanted to delegate translation loading to a translatable module's parent class ctor in order to share code and, more importantly, ensure they are loaded before any widgets are created.

    The following doesn't work - _action's text is untranslated "Welcome":
    Qt Code:
    1. class Translatee
    2. {
    3. public:
    4. Translatee(const QString &libname)
    5. {
    6. QString path("/path/to/qm/files/");
    7. QTranslator translator;
    8. bool ok = translator.load(libname + "_fr", path);
    9. ok &= qApp->installTranslator(&translator);
    10. //ok == true
    11. }
    12. }
    13.  
    14.  
    15. class FooModule : public QObject
    16. , public Translatee
    17. {
    18. Q_OBJECT
    19. QAction *_action;
    20. public:
    21. FooModule(QObject *parent)
    22. : QObject(parent)
    23. , Translatee(metaObject()->className().toLower())
    24. {
    25. _action = new QAction(tr("Welcome"), this);
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 

    This does work - _action's text is "Bienvenue" as expected:
    Qt Code:
    1. class FooModule : public QObject
    2. {
    3. Q_OBJECT
    4. QAction *_action;
    5. public:
    6. FooModule(QObject *parent)
    7. : QObject(parent)
    8. {
    9. QString libname(metaObject()->className().toLower());
    10. QString path("/path/to/qm/files/");
    11. QTranslator translator;
    12. bool ok = translator.load(libname + "_fr", path);
    13. ok &= qApp->installTranslator(&translator);
    14.  
    15. _action = new QAction(tr("Welcome"), this);
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    Supplement:
    Must be a scope issue rather than an execution order issue:
    Qt Code:
    1. class Translatee
    2. {
    3. public:
    4. static bool load(const QString &libname)
    5. {
    6. QString path("/path/to/qm/files/");
    7. QTranslator translator;
    8. bool ok = translator.load(libname + "_fr", path);
    9. ok &= qApp->installTranslator(&translator);
    10. return ok;
    11. }
    12. }
    13.  
    14.  
    15. class FooModule : public QObject
    16. {
    17. Q_OBJECT
    18. QAction *_action;
    19. public:
    20. FooModule(QObject *parent)
    21. : QObject(parent)
    22. {
    23. bool ok = Translatee::load(metaObject()->className().toLower());
    24.  
    25. _action = new QAction(tr("Welcome"), this);
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 
    This doesn't work either.

    Why is this? Is there a way to delegate translation loading to before the module's ctor body is executed?
    Last edited by zaphod.b; 6th January 2014 at 17:05.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: translation loading / execution order / QObject magic(?)

    The first examples fails because your QTranslator instance goes out of scope and is destroyed at the end of the constructor.
    The second works because the QTranslator is still in-scope at the time tr() is called for the action's text. It then goes out of scope and is destroyed so subsequent translations will not work.
    Supplement: See first and second example regarding scope

    You avoid this problem but ensuring the translator stays in scope for at least as long as you need to use it. The examples all allocate it on the stack in main() so that the translator exists for the entire program lifetime. You do not have the luxury of stack allocation in main() but you can heap allocate your QTranslator with an appropriate QObject parent.

  3. The following user says thank you to ChrisW67 for this useful post:

    zaphod.b (6th April 2014)

  4. #3
    Join Date
    Jun 2006
    Posts
    81
    Thanks
    6
    Thanked 5 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: translation loading / execution order / QObject magic(?)

    I checked the sources before posting:
    Qt Code:
    1. bool QCoreApplication::installTranslator(QTranslator *translationFile)
    2. {
    3. if (!translationFile)
    4. return false;
    5.  
    6. if (!QCoreApplicationPrivate::checkInstance("installTranslator"))
    7. return false;
    8. QCoreApplicationPrivate *d = self->d_func();
    9. d->translators.prepend(translationFile);
    10.  
    11. #ifndef QT_NO_TRANSLATION_BUILDER
    12. if (translationFile->isEmpty())
    13. return false;
    14. #endif
    15.  
    16. #ifndef QT_NO_QOBJECT
    17. QEvent ev(QEvent::LanguageChange);
    18. QCoreApplication::sendEvent(self, &ev);
    19. #endif
    20.  
    21. return true;
    22. }
    To copy to clipboard, switch view to plain text mode 
    So QCoreApplication::installTranslator() basically adds the QTranslator to an internal list.

    I always was under the impression that it is a copy that is added to a list. Also I assumed (but couldn't verify so far) that the translations are taken from the (list of) installed translators. In this case the original QTranslator instance going out of scope shouldn't matter, should it?

    Anything wrong with my train of thought?

    Edit:
    Oops, a copy of the pointer... Shame on me
    Sometimes I get lost in a mental deadlock...
    Last edited by zaphod.b; 7th January 2014 at 13:38.

Similar Threads

  1. Replies: 1
    Last Post: 27th January 2012, 09:33
  2. Libcurl messes up the order of execution
    By phoenix12345 in forum Qt Programming
    Replies: 2
    Last Post: 7th September 2010, 11:14
  3. Execution order
    By Ishtar in forum Newbie
    Replies: 2
    Last Post: 26th April 2010, 20:36
  4. QProcess magic
    By baray98 in forum Qt Programming
    Replies: 1
    Last Post: 15th August 2008, 06:18
  5. Magic of Virtual Mechanism in C++
    By joseph in forum General Programming
    Replies: 2
    Last Post: 21st January 2008, 13:22

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.