Another "change qt application language at runtime" thread.



  • Hello Qt devs!

    I have tried following "this guide":http://developer.qt.nokia.com/wiki/How_to_create_a_multi_language_application in order to achieve my goal. I'm having a hard time figuring it out though.

    A short explanation of my Qt application:
    I have like a base template (bt) which is inherited by all my ui's (Some have another class in between which also is a template adding navigation buttons).
    In the bt, I implement these function, borrowed from the link in the top of the post (with some changes):

    @void BasicSetup::languageChanged(QString language) {
    loadLanguage(language);
    }

    void BasicSetup::switchTranslator(QTranslator& translator, const QString& filename) {
    // remove the old translator
    qApp->removeTranslator(&translator);

    // load the new translator
    if (translator.load(filename))
    qApp->installTranslator(&translator);
    }

    void BasicSetup::loadLanguage(const QString& rLanguage) {

    QString m_currLang = "eng.qm"; //this is hardcoded for testing...
    QTranslator m_translator;
    QTranslator m_translatorQt;

    if (m_currLang != rLanguage) {
    m_currLang = rLanguage;
    QLocale locale = QLocale(m_currLang);
    QLocale::setDefault(locale);
    QString languageName = QLocale::languageToString(locale.language());
    switchTranslator(m_translator, rLanguage);
    switchTranslator(m_translatorQt, rLanguage);
    }
    }

    void BasicSetup::changeEvent(QEvent* event)
    {
    if (event->type() == QEvent::LanguageChange)
    {
    // retranslate designer form
    ui.retranslateUi(this);

        // retranslate other widgets which weren't added in designer
        //retranslate(); //Only testing ui designer text
    }
    
    // remember to call base class implementation
    QWidget::changeEvent(event);
    

    }@

    I was then hoping to be able to call:
    @languageChanged("dan.qm");@
    from one of the child ui's.

    • in order to change the entire application language.
      Sadly this is not working out for me. The label text which I overwrite in code does go back to its "default value" which it has in the designer. (E.x. "Textlabel" which is the default when dragging in a textlabel)
      What am I missing?

    Thank you for your time!



  • Ok, there is a concurrent between two things: the automatical way (with static value) and the programatic way.

    I propose that you add a function: @virtual void BasicSetup::translationChanged() {}@

    Of course to each time you need a contextual handling of the translation reimplement this function:
    @
    virtual void MyComplexUi::translationChanged()
    {
    myLabelInUI->setText(is_loaded ? tr("loaded") : tr("empty"));
    }
    @

    And of course empty the label to avoid an unnecessary double set of the value.



  • I forget to bind it ...

    @
    void BasicSetup::changeEvent(QEvent* event)
    {
    if (event->type() == QEvent::LanguageChange)
    {
    // retranslate designer form
    ui.retranslateUi(this);

        // not automatally retranslation
        translationChanged();
    }
    
    // remember to call base class implementation
    QWidget::changeEvent(event);
    

    }
    @



  • But how should this change the fact that I don't get the translation from my .qm file?

    I'm confused...



  • I'm using myself a similar mechanism, the tr() will use the values of your new loaded dictionary.

    The values not set in designer will simply not appear in the retranslateUI(), you should simply clear the content of your label in the designer and use alternatively the translationChanged() to handle the non-static cases.



  • Clearing the content of my label in the designer does not change anything it seems... The label just becomes empty when when trying to change language.

    Also, I'm sure what the translationChanged() function does? Doesn't it just overwrite whatever is in the label with "loaded" or "empty"?



  • Your code works perfectly, if you put in designer Textlabel as 'text' entry, the retranslatedUI() will simply find the equivalent in the string map (of your translator) and replace it and if none found he reset to the original value.

    Which means probably: it's missing the translation of your text?



  • Oh but the label has dynamic content.

    The text is set when I create an instance of my ui. It takes a parameter called "title". It's this variable which should be translated and showed in the label.

    @TemplateUi::TemplateUi(QString title, QWidget *parent) :
    QWidget(parent) {
    ...
    }@



  • move this code from the c'tr to the 'famous' routine:

    void translationChanged() { mylabel->setText(tr("title")); }



  • what you dream is that Qt detects the 'title' with something like: @mylabel->setText(QApplication::translate(mylabel->text()));@

    Now remarks:

    • how he could find the translation if the text() is chinese?
    • how you will untranslate text like: tr("It will reboot in %1, please save your work!").arg(time) ?


  • I want to translate my gui to danish. My default language is english.
    I expect that Qt looks up my title ("Main menu") i my "dan.qm" file, and exchange it with the danish equivalent ("Hovedmenu").

    When creating my screen/ui object, I do this:

    @MenuList mainMenuScreen = new MenuList(tr("Main menu"), &mainMenuListVector);@

    The first parameter is the title.



  • This all should be done in 3 parts:

    a) Application
    @
    class MyApp
    {
    QTranslator m_translator;

    public:
    MyApp()
    {
    installTranslator(&m_translator);
    }
    MyApp::changeTranslation()
    {
    m_translator.load(QFileDialog::getOpenFileName());//here complete ()
    }
    };
    @

    b) make automatical response to LanguageChange message
    @
    class MyWidget : public QWidget, public Ui::MyWidget
    {
    QLable* m_mainMenuScreen;

    public:
    MyWidget(QWidget* parent)
    {
    setupUI(this);

    m_mainMenuScreen = new MenuList(this, &mainMenuListVector); // don't care here with i18n
    

    }

    protected:
    virtual void translationChanged()
    {
    m_mainMenuScreen->setText(tr("Main menu"));
    }

    virtual void changeEvent(QEvent* event)
    {
    if (event->type() == QEvent::LanguageChange)
    {
    // retranslate designer form
    ui.retranslateUi(this);

        // not automatally retranslation
        translationChanged();
    }
    
    // remember to call base class implementation
    QWidget::changeEvent(event);
    

    }
    };
    @

    C) Now the goal is to make all this simplier, because of course it's not confortable to have to create manually for each widget all the routine. An idea is to inherit your own interface that you construct too in the constructor, this interface could install an event filter, here my quick and dirty and fast solution, not simply copy, check more the ideas:

    @
    template <class T>
    class MyTranslation : public QObject
    {
    T* m_t;

    public:
    MyTranslation() : m_t(0) {}
    ~MyTranslation() { if (m_t) m_t->QWidget::removeEventFilter(this); m_t = 0; }

    void installTranslator(T* t) { m_t = t; m_t->QWidget::installEventFilter(this); }
    

    protected:
    virtual void translationChanged() {}

    bool eventFilter(QObject *obj, QEvent *event)
    {  
        if (m_t && event->type() == QEvent::LanguageChange)
        {
            // retranslate designer form
            m_t->retranslateUi(this);
    
            // not automatally retranslation
            translationChanged();
        }
        return false;
    }
    

    };

    class MyWidget : public QWidget, public Ui::MyWidget, public MyTranslation<MyWidget>
    {
    QLabel* m_mainMenuScreen;

    public:
    MyWidget(QWidget* parent) : QWidget(parent)
    {
    // construct
    setupUI(this);
    m_mainMenuScreen = new QLabel(this); // don't care here with i18n

        installTranslator(this);
    }
    

    protected:
    virtual void translationChanged()
    {
    m_mainMenuScreen->setText(QWidget::tr("Main menu"));
    }
    };
    @



  • Thanks a lot for your replies BilbonSacquet.

    I'm afraid I'm rather thickheaded...

    You make me very confused. In your previous post - the goal for me is to make it like shown in c), right? a) and b) are what? Just to show the difference?

    So in c) you tell me that all my screen widgets, should inherit from this MyTranslation interface? And why inherit from Ui::MyWidget? I just don't get it.

    And what about the EventFilter - what is that for?

    My brain is soon gonna pop out of my skull.

    Can you, or anyone else, explain it to me in other words? Or just spell it out more.

    I'm sorry :(

    But thank you so much for your time!



  • ok, I go perhaps too fast, I'm stressed right now with work and I will to help the directest way possible. But that's my bad.

    That's true a) b) is just to explain the steps ... and c) the solution.

    Yes, all widget which needs translation should inherits this class, that's necessary to take the message QEvent::LanguageChange and to make 'automatically' callback the translation functions. The event filter is necessary because the template MyTranslation doesn't inherit of the widget (it's just an QObject) function like event().

    The message LanguageChange cycle is now:
    app -> MyWidget::event() -> MyTranslator::eventFilter() -> MyWidget::Ui::MyWidget::retranslateUI() , MyWidget::translationChanged()
    (if the function is redefined otherwise MyTranslator::translationChanged())

    Now why I inherits from Ui::MyClass, you will get the information from the Qt help. But mainly it makes all simplier in the handle of your widgets: same interface, you have all your children direct, you could use the on_mywidget_signal() slots without to connect/disconnct anything.
    What I need is that all the widget has the function retranslateUi() accessible in the interface to be called by the template.

    I hope that I have helped you with the explanation and not make all darker like in the Morian mines :D



  • Okay I'm trying to work my way through it.

    I'm writing the MyTranslation class now, but I get some errors:

    @template <class T>
    class MyTranslation : public QObject
    {
    T* m_t; //<------------- "Type 'T' could not be resolved"

    public:
    MyTranslation() : m_t(0) {}
    ~MyTranslation() { if (m_t) m_t->QWidget::removeEventFilter(this); m_t = 0; }
    ...@

    Do I need to include something in order to do this?



  • Now I have verified/compiled the prototype given, I had done an error m_t->retranslateUi(-his- m_t);

    Translation template - MyTranslator.h:
    @
    #include <QtCore/QObject>

    template <class T>
    class MyTranslation : public QObject
    {
    T* m_t;

    public:
    MyTranslation() : m_t(0) {}
    ~MyTranslation() { if (m_t) m_t->QWidget::removeEventFilter(this); m_t = 0; }

    void installTranslator(T* t) { m_t = t; m_t->QWidget::installEventFilter(this); }
    

    protected:
    virtual void translationChanged() {}

    bool eventFilter(QObject *obj, QEvent *event)
    { 
        if (m_t && event->type() == QEvent::LanguageChange)
        {
            // retranslate designer form
            m_t->retranslateUi(m_t);
    
            // not automatally retranslation
            translationChanged();
        }
        return false;
    }
    

    };
    @

    The widget - MyWidget.h:

    @
    #include <QtGui/QWidget>

    #include <ui_MyWidget.h>
    #include <MyTranslator.h>

    class MyWidget: public QWidget, public Ui::MyWidget, public MyTranslation<MyWidget>
    {
    Q_OBJECT

    public:
        MyWidget(QWidget* parent = 0);
        virtual ~MyWidget();
    
    protected:
        virtual void translationChanged();
    

    };
    @

    The MyWidget.cpp:
    @
    #include "MyWidget.h"

    MyWidget::MyWidget(QWidget* parent) : QWidget(parent)
    {
    setupUi(this);
    installTranslator(this);
    }

    MyWidget::~MyWidget() {}

    void MyWidget::translationChanged()
    {
    qWarning() << "translator changed";
    }
    @

    And of course you need a MyWidget.ui with class MyWidget :) use the default widget. To make it faster I integrate on my current project, and redo a adapted copy/paste. It could have typos but should be ok now.



  • Thank you, but I still get the error in the start when writing:

    @T* m_t;@



  • hmm .. which compiler are you using ?



  • mingw



  • you could try to replace 'template <class T>' by 'template <typename T>'



  • No change :(



  • and for no warnings compilation add: Q_UNUSED(obj); into the enventFilter()



  • But the error happens before this? I have only written:

    @template <class T>
    class MyTranslation : public QObject
    {
    T* m_t;

    public:
    MyTranslation() : m_t(0) {}
    ~MyTranslation() { if (m_t) m_t->QWidget::removeEventFilter(this); m_t = 0; }
    };@

    And it gives the error...



  • I have found some help in internet, seems that your interface (eclipse) not the compiler the problem.

    "here some indication":stackoverflow.com/questions/6503551/opencv-type-iplimage-could-not-be-resolved



  • Thank you :)

    I wrote the code in Qt Creator instead of Eclipse, and no errors appear in the MyTranslation.h file.

    I am now trying to set up the "MyWidget.h" file.

    First of all I can't inherit from Ui::MyWidget. Can this have something to do with MyWidget being a base class for all my ui's?

    Secondly, if I leave out Ui::MyWidget, but inherit from MyTranslation<MyWidget>, my ui's seems to get errors, when it comes to the signal and slot connections made.

    "reference to 'connect' is ambiguous"



  • If you couldn't inherits from the Ui::MyWidget you should adapt the code in the handler to find the restranslateUI(), or move the retranslateUI() call in the translationChanged() function.

    yes, because you have 2x QObject inheritence. Use at the place the on_awidget_asignal() slots or in this case use QWidget::connect().



  • I'm sorry, I don't quite understand what you are saying? :(



  • Sorry, regarding the way I do multiple inheritence, you could find help in the Qt documentation: "Using a Design UI File in Your Application", pay a look to the last chapter:"Automatic Connections".



  • The link you gave me says

    @QMetaObject::connectSlotsByName(this);@

    Gives me this error: “'QObject' is an ambiguous base of 'MyWidget'”

    • But you use QWidget:: Is there a reason for this?


  • You inherit twice from QObject:

    @
    class MyTranslation : public QObject
    class MyWidget: public QWidget, public Ui::MyWidget, public MyTranslation<MyWidget>
    @

    So you inherit from QObject via QWidget (which is a QObject subclass) and via MyTranslation. This is not supported!



  • BilbonSacquet you must have met this problem then?



  • It's supported, you should simply take care of the conflict. But we could do it through delegations (I take in consideration all what we say):

    @
    #include <QtCore/QObject>

    template <class T>
    class MyTranslator : public QObject
    {
    T* m_t;

    public:
    MyTranslator(T* widget)
    : m_t(widget)
    {
    m_t->installEventFilter(this);
    }

    ~MyTranslator() 
    { 
        m_t->removeEventFilter(this); 
    }
    
    bool eventFilter(QObject *obj, QEvent *event)
    {
        Q_UNUSED(obj);
    
        if (m_t && event->type() == QEvent::LanguageChange)
        { 
            // not automatally retranslation
            m_t->T:translationChanged();
        }
        return false;
    }
    

    };

    #include <QtGui/QWidget>

    #include <ui_MyWidget.h>
    #include <MyTranslator.h>

    class MyWidget: public QWidget
    {
    Q_OBJECT

    public:
        MyWidget(QWidget* parent = 0)
        : QWidget(parent), 
          m_translator(this)
        {
            ui.setupUi(this);
        }
    
        virtual ~MyWidget() {}
    
        virtual void translationChanged()
        {
            ui.restranslateUi();
    
            qWarning() << "translator changed";
        }
    
    private:
        MyTranslator<MyWidget> m_translator;
        Ui::MyWidget ui;
    

    };
    @



  • The "docs of moc":http://doc.qt.nokia.com/4.7/moc.html#multiple-inheritance-requires-qobject-to-be-first state it clearly:

    bq. Multiple Inheritance Requires QObject to Be First
    If you are using multiple inheritance, moc assumes that the first inherited class is a subclass of QObject. Also, be sure that only the first inherited class is a QObject.
    [emphasis by me, Volker]



  • But now you don't inherit from MyTranslator, in the code you just postet?



  • Yes, I do both variation (just for you)!

    The inherited and the delegated! Both are correct ... just now choose the one you want :).



  • Even the multiple inherited is correct because (to take the sitation of Volker) the first inherited IS a QObject (QWidget is a QObject)!! :)



  • [quote author="BilbonSacquet" date="1321629111"]Even the multiple inherited is correct because (to take the sitation of Volker) the first inherited IS a QObject (QWidget is a QObject)!! :)[/quote]

    Don't pick what you want to have and leave out the annoying details that make your design fail!

    bq. Also, be sure that only the first inherited class is a QObject.



  • Both was tested and works! :)

    But I give you right, it's never good to multiple inherits QObject. So that's why I rewrite it with a delegation.

    But in fact to achieve some automatical translation mechanism (without to hack the current implementation) we need in QObject a callback like in widget for paint event with paintEvent().



  • working != officially supported

    And the state of "working" may vary between platforms and compilers.

    And I don't get the purpose of the template class at all. It calls retranslateUi in the templates argument, so it has to be implemented in all the widgets. Where's the big difference and the coder's savings to implementing changeEvent? On will have to touche every widget class eventually, so why not make it the "standard" way?



  • You have right not much, I have tried to make something lighter through template (like he suggests) and to have not to write too much code. :)

    For the main problem, you should retranslate anyway yourself the 'dynamical' strings.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.