Important: Please read the Qt Code of Conduct -

Dynamic translation of strings in widgets

  • I understand that for strings that I implement in my code using tr(), I must re-send them on a languageChange event, but how do strings that are entered as part of a widget get re-translated? Is there some way to call the widget, or the window they are on to automatically re-translate them? Will strings get re-translated if hide() and then show() are called on the widget or window? Or should I just programatically generate all strings and then re-translate them myself?

  • If you need dynamic translation you have to write your code to support it.


  • @alex_malyu thanks for the pointer to the informit article.
    I already have my code designed for translations. My question is specifically, for things like a QButton, is the text in the 'text' property automatically handled by the system, or do I need to explicitly translate all text?
    It appears that I need to explicitly translate all text, and therefore there is no reason to enter any default text into the text property of any widget as the widget doesn't handle the LanguageChange event by itself, even though it will handle translation of the default string when it is first displayed.
    I was also asking if there was any way to force a widget to essentially re-translate it's text property by, maybe, calling hide() and then show() on the widget. Sounds like that won't work.
    Handling of the default value of the 'text' property is not explicitly dealt with in any article I've been able to find on dynamic translation.

  • Sorry for the double post, I din't know that I had hit send twice.

  • On further investigation, the UI code of a widget creates a retranlsateUi() function for all translatable text fields in any widgets on a window. So now the question is, how do you call that function?
    The setupUi() function is called through the ui parameter that is added to any QWidget derived object, so the retranslateUi() function can be called in the same way, ui->retranslateUi().
    This means that any translatable text contained in any widgets on a window can be re-translated by calling the ui->retranslateUi() function, which can be done in the users retranslateUi() function called from the LanguageChange event handler.
    It would be nice if this was documented somewhere and mentioned in the Qt article on Dynamic Translation.

  • Lifetime Qt Champion


    Do you mean the retranslateUI part should be added to that chapter ?

  • That was covered in the links I gave you. look for Dynamic Translation

  • @alex_malyu Generating your own retranslateUi() was covered in the links that you sent me, no where that I have found is it mentioned that each of the Ui_xxx.h files that are generated for each window that your program has contains a retranslateUi() function that you can call to re-translate the default values that have been entered for 'text' properties of widgets in your window.

  • @SGaist I think that the fact that the Ui compiler generates a retranslateUi() function for each window should be mentioned. I've been trying to figure out if I needed to change all of my widget that I have entered default values for the 'text' property, and it turns out that the Ui compiler creates a retranslateUi() function, but it is not documented in the section on Dynamic Translation.

  • Below is an answer.

    void MyWidget::changeEvent(QEvent *event)
    if (e->type() == QEvent::LanguageChange) {
    titleLabel->setText(tr("Document Title"));
    } else

    code within if (e->type() == QEvent::LanguageChange) { ... )
    should contain ALL the code which has to be called when language changed.
    Basically every line of the code with tr() has to be called there.

    In there you can put call to retranslateUi,
    but retranslateUi is just a function where QtDesigner put strings created in there.
    this might be or might not be sufficient for your widget.
    Finally there are people who do not use designer.
    That is why you could not find retranslateUi mentioned anywhere.
    Keyword is tr().

    For example you may fill tree widget outside of designer.
    If you used text which has to be internationalized there, you need to make sure the same code is executed as a reaction on language changed.

    Common logic suggests to put all code which requires initialization in a single function or slot.
    Such function or slot should be called when QEvent::LanguageChange arrives.
    Call this function myRetranslateUi if you want.

    You do not have to subclass every widget (as suggested by example) though.
    Alternative is to install event filter.