简体   繁体   中英

how to translate string added dynamicaly in Qapp with Qtranslator?

i trying to created a Qt application multilanguage with Qt linguist. I place this code in an function of my MainWindow :

translator.load(":/lang/English");
qApp->installTranslator(&translator);
ui->retranslateUi(this);

With the QTranslator declare in my MainWindow.h and all my string i want translate enclose by tr() . But with that, all QObject added dynamicaly by the code of my MainWindow.cpp, for example the title of a QTableWidget, are not translated. If i place an other translator in my main.cpp, all my string are translate but i must created language button in my application and so i can't place translator in main.cpp.

Do you have an idea to help me?

Thx for your answers.

Gat

When you add a translation in your application using qApp->installTranslator(& aTranslator) then all the following calls to QObject::tr() (and similar functions) will look up in the translator for a translated text. So you should call retranslateUi() after qApp->installTranslator() . Actually you might event not call it there, you may reimplement QWidget::changeEvent() and intercept any QEvent::LanguageChange event.

void MainWindow::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        // Someone called qApp->installTranslator() with a new translation.
        // Let's update the user visible strings.

        // This function was created by uic from the Designer form.
        // It translates all user visible texts in the form.
        ui->retranslateUi(this);

        // This function must be written by you. It translates other user visible
        // texts that are not in the form. See down for an example.
        this->retranslateUi();
        break;
    default:
        break;
    }
}

ui->retranslateUi() just calls QObject::tr() for each user visible string in the ui. It is called automatically at the end of setupUi() and populates the form's widgets with translated text (have a look, it is defined by uic in ui_MainWindow.h file). You may want to take a similar approach with other user visible texts, like the title of a QTableWidget. All the strings are set in a function (named perhaps retranslateUi() for consistency) which is called at application starts (or, better, after the relevant widgets are created) and every time a new translations is loaded.

MainWindow::MainWindow(QWidget * parent)
  : QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // Creates other widgets, but do not set their user visible texts.
    tableWidget = new QTableWidget(this);
    ...
    someControl = new QLineEdit(this);
    someOtherControl = new QSpinBox(this);

    someModel = new MyModel(this);
    ...


    // Ok, *now* we set their texts.
    this->retranslateUi();
}

...

void MainWindow::retranslateUi()
{
    // This function will be called (either manually or automatically by
    // `changeEvent()`) when a new translator has installed, hence all the `tr()`
    // calls will return the right translation for the last installed language.

    QStringList labels;
    labels.append(tr("First column"));
    labels.append(tr("Second column"));
    labels.append(tr("Third column"));

    tableWidget->setHorizontalHeaderLabels(labels);

    someControl->setText(tr("Control name"));
    someOtherControl->setText(tr("Other control name"));

    // Perhaps you have a model that need to be retranslated too
    // (you need to write its `retranslateUi()` function):
    someModel->retranslateUi();

    ...
}

Also, please note that if you are doing something like

void MainWindow::someFunction()
{
    ...

    QTranslator translator;
    translator.load(":/lang/English");
    qApp->installTranslator(& translator);

    ...
}

as soon as that function returns the variable translator gets destroyed, so next time QObject::tr() is called the address stored with qApp->installTranslator(& translator) will be invalid. So you must allocate translator on the heap with new (and possibly deleting it with delete when you do not need it anymore). An exception is if you are doing that in main() before calling QCoreApplication::exec() since that function is blocking and will not return until application is closed.

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    ...

    QTranslator translator;
    translator.load(":/lang/English");
    app.installTranslator(& translator);

    ...

    app.exec(); // This function will return only on application's exit.
                // Hence `translator` will outlive the application, there is
                // no need to worry about it.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM