I integrated the QTranslator
class in my Project. So far everything works and on restart of the program all text fields are translated. Now I would like to provide dynamic translation, so the users don't need to restart the application.
What I found in my research is that its necessary to reimplement the changeEvent()
function like this:
void MyWidget::changeEvent(QEvent *event)
{
if (event->type() == QEvent::LanguageChange) {
titleLabel->setText(tr("Document Title"));
...
okPushButton->setText(tr("&OK"));
} else
QWidget::changeEvent(event);
}
(Source: http://doc.qt.io/qt-5/internationalization.html#dynamic-translation)
For applications written with Qt designer it seems like its possible to just call
ui->retranslateUi(this);
within the changeEvent()
function and all text fields will be translated.
But for all other texts in the application the text must be set as in the example above. Which I find painful because I always need to update the text at two places when I change something (in the changeEvent
function and in the main part of my program).
With a lot of text fields it could easily happen to miss something.
Is there a way to update these text fields without the need to duplicate the "text-setting-methods"?