Dynamic Language Translation in Qt
I have written one application in that application i have to do reboot the system then only after language translation applied.
What i want to change the language without reboot i mean at run time i have to change the all text in translated localized form.
In my sample application in main i have written following code.
QTranslator * translator;
translator = new QTranslator;
translator->load( "QM file path where the language file exist ");
From another file or form how can i know the language has changed and how to handle it ?
Can someone explain me how to do with this code ? or sample code or link will helped me a lot.
[Edit] Use @ for code
You need to react on the language change event within your widgets. The change event is posted to all widgets if there is a change to the installed Qt translator.
@ void MyWidget::MyWidget(QWidget* parent)
void MyWidget::changeEvent(QEvent* event)
if (event->type() == QEvent::LanguageChange)
// retranslate designer form
// retranslate other widgets which weren't added in designer retranslate(); } // remember to call base class implementation QWidget::changeEvent(event);
Sorry for my intrusion...I've just located an entire desktop app, but I'm able to see translations just for my texts and not for ui generated texts.
shall I have to use dynamic translation to change ui texts?!
Some time ago I did it as follows;
First create actions for different languages and connect their triggered() SIGNALs with language specific SLOTs.
MainWindow::MainWindow(QWidget *parent) :
connect(ui->actionDanish, SIGNAL(triggered()), this, SLOT(changeLanguageToDanish()));
connect(ui->actionEnglish, SIGNAL(triggered()), this, SLOT(changeLanguageToEnglish()));
Then from that SLOTs call to language change function.
Load the language file
@void MainWindow::changeLanguageTo(const QString &language_file_location)
Call language change event
@void MainWindow::changeEvent(QEvent *e)
if(e->type() == QEvent::LanguageChange)
So I have to do something to translate ui....I've tried to implement changeEvent for one ui file and now it is correctly located.
My question is: why my tr(text) in the code is automatically translated at runtime and ui generated tr did not?
@ AlterX ,
what do you mean by ui generated?
is it means the text of buttons, labels, menus etc?
it creates ui_xxxx.h with all localized code QApplication::translate(), but even they are present like my own code, ui form doesn't get translated at runtime like my code does!
generally I set the text of button as _[Click Me] _.
Then by using 'Qt Linguist' set the text in different languages for [Click me] .
And I'm not doing any other thing except my previous reply. ChangeEvent do the rest.
Ok so changeEvent is necessary to translate uis...