I am working on a Qt project consisting of QMainWindow and several Qt and non-Qt classes. Many of them use QStrings with tr() , which are translated using Qt Linguist. Changing the language ( QTranslator download and install / QTranslator download and uninstall) is started by QActions in the application menu.
I read the official Qt documentation regarding dynamic translation and basically offers the following overload:
void MainWindow::changeEvent(QEvent *event) { if (event->type() == QEvent::LanguageChange) { titleLabel->setText(tr("Document Title")); ... // all my tr() QStrings here okPushButton->setText(tr("&OK")); } else QWidget::changeEvent(event); }
The problem I am facing is that QStrings for translation are many (only 58 in QMainWindow ), and some are populated at runtime, as well as through user interaction; QPushButton . myFunction(a,b) below is called via QPushButton :
void MainWindow::myFunction(MyClassA a, MyClassB b) { ... if(b.myCondition() == 0) { ... // below is the problem myLabel->setText(myLabel->text() + QString("\n" + a->getName() + tr(" gagne ") + exp + tr(" points d'expérience"))); } else { myLabel->setText(QString(tr("something else"))); } ... }
Therefore, I hardly see how to include this type of QString in the changeEvent() method described above. What about classes outside MainWindow that also have QStrings for translation but are not QWidget (so no changeEvent overload is possible)?
I read that there is another way to use this method with a user interface form:
void MainWindow::changeEvent(QEvent* event) { if (event->type() == QEvent::LanguageChange) { ui.retranslateUi(this); } ... }
But this is due to the fact that I use the UI form in my project, which I do not do (all widgets are created in the code). I tried to export my MainWindow in the form of a user interface, but when I try to include the generated header in the project, I get the following error:
ui_fenetreprincipale.h:32: error: qmainwindowlayout.h: No such file or directory
Thank you for your preliminary offer to choose the best way to translate my application.