Important: Please read the Qt Code of Conduct -

Change language at runtime

  • Hi! I'm writing my application by code and I have implemented language change in runtime but I don't have ui.retranslateUi(this) function to change text. I have managed to change some text but problem is how to change combobox items text? Thanks in advance for your help.

  • @Cobra91151
    Hi, friend, welcome devnet.

    There is my way. I hope it can help you.

    1. First, Tow slots function when changed the language
    void MainWindow::StActionLanguageChina()
        mpTranslator->load(":/lg/language/lg_ch.qm");// QTranslator load the language file
    void MainWindow::StActionLanguageEnglish()
    1. rewrite the changeEvent function
    void MainWindow::changeEvent(QEvent *event)
        if( QEvent::LanguageChange == event->type() )
            ReUiSetText(); // note this function: if you hasn't the ui file, must use this way to translate
    1. If hasn't ui file, should use the way.
    void MainWindow::ReUiSetText()
        //...all the language what you want to translate, but not has the ui file. you should rewrite the `set....` at here. to change the language.

  • @joeQ
    Thanks but you misunderstood my question. I have already implemented my function Test::appLocalization() in which I change text, the problem is how to change QComboBox items text?

    QComboBox did not have property setTitle.

  • You basically have to re-execute any line of code that calls tr() so in case of QComboBox:

    // QComboBox* comboBox
    auto comboModel = comboBox.model();

  • Qt Champions 2017

    That's one thing the designer shines at - retranslating the whole of the static UI automagically ... :)

  • @VRonin

    I have changed your code to:
    comboBox->model()->setData(comboBox->model()->index(0,0), QObject::tr("Text"));

    And now it's retranslating QComboBox. I post it here so others can find the solution. Thank you for your reply.

Log in to reply