Another “change qt application language at runtime” thread.
Page |
1 |
Hello Qt devs!
I have tried following this guide [developer.qt.nokia.com] 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):
- loadLanguage(language);
- }
- // remove the old translator
- qApp->removeTranslator(&translator);
- // load the new translator
- if (translator.load(filename))
- qApp->installTranslator(&translator);
- }
- QTranslator m_translator;
- QTranslator m_translatorQt;
- if (m_currLang != rLanguage) {
- m_currLang = rLanguage;
- switchTranslator(m_translator, rLanguage);
- switchTranslator(m_translatorQt, rLanguage);
- }
- }
- {
- {
- // 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
- }
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!
49 replies
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’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.
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()
- {
- }
- };
b) make automatical response to LanguageChange message
- {
- QLable* m_mainMenuScreen;
- public:
- {
- setupUI(this);
- m_mainMenuScreen = new MenuList(this, &mainMenuListVector); // don't care here with i18n
- }
- protected:
- virtual void translationChanged()
- {
- m_mainMenuScreen->setText(tr("Main menu"));
- }
- {
- {
- // retranslate designer form
- ui.retranslateUi(this);
- // not automatally retranslation
- translationChanged();
- }
- // remember to call base class implementation
- }
- };
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>
- {
- T* m_t;
- public:
- MyTranslation() : m_t(0) {}
- protected:
- virtual void translationChanged() {}
- {
- {
- // retranslate designer form
- m_t->retranslateUi(this);
- // not automatally retranslation
- translationChanged();
- }
- return false;
- }
- };
- {
- public:
- {
- // construct
- setupUI(this);
- installTranslator(this);
- }
- protected:
- virtual void translationChanged()
- {
- }
- };
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
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>
- {
- T* m_t;
- public:
- MyTranslation() : m_t(0) {}
- protected:
- virtual void translationChanged() {}
- {
- {
- // 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>
- {
- Q_OBJECT
- public:
- virtual ~MyWidget();
- protected:
- virtual void translationChanged();
- };
The MyWidget.cpp:
- #include "MyWidget.h"
- {
- 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.
You must log in to post a reply. Not a member yet? Register here!
