简体   繁体   English

如何使用Qtranslator在Qapp中翻译字符串添加dynamicaly?

[英]how to translate string added dynamicaly in Qapp with Qtranslator?

i trying to created a Qt application multilanguage with Qt linguist. 我试图用Qt语言学家创建一个Qt应用程序多语言。 I place this code in an function of my MainWindow : 我将此代码放在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() . 随着QTranslator在我的MainWindow.h中声明我所有的字符串我希望翻译由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. 但是,通过我的MainWindow.cpp的代码添加动态的所有QObject,例如QTableWidget的标题,都没有被翻译。 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. 如果我在main.cpp中放置了另一个翻译器,我的所有字符串都是翻译的,但我必须在我的应用程序中创建语言按钮,所以我不能将翻译器放在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. 当您使用qApp->installTranslator(& aTranslator)在应用程序中添加翻译时,所有以下QObject::tr() (以及类似函数)的调用都将在翻译器中查找翻译文本。 So you should call retranslateUi() after qApp->installTranslator() . 所以你应该 qApp->installTranslator() 之后调用retranslateUi() qApp->installTranslator() Actually you might event not call it there, you may reimplement QWidget::changeEvent() and intercept any QEvent::LanguageChange event. 实际上你可能不会在那里调用它,你可以重新实现QWidget::changeEvent()并拦截任何QEvent::LanguageChange事件。

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. ui->retranslateUi()只为ui中的每个用户可见字符串调用QObject::tr() 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). 它在setupUi()的末尾自动调用,并使用已翻译的文本填充表单的小部件(看看,它由ui_MainWindow.h文件中的uic定义)。 You may want to take a similar approach with other user visible texts, like the title of a QTableWidget. 您可能希望采用与其他用户可见文本类似的方法,例如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. 所有字符串都在一个函数中设置(可能名称为retranslateUi()以确保一致性),该函数在应用程序启动时调用(或者,更好地,在创建相关小部件之后)以及每次加载新的翻译时调用。

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. 一旦该函数返回,变量translator就会被销毁,因此下次调用QObject::tr()使用qApp->installTranslator(& translator)存储的地址将无效。 So you must allocate translator on the heap with new (and possibly deleting it with delete when you do not need it anymore). 因此,您必须在堆上使用new分配translator (并且可能在不再需要时使用delete它)。 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. 一个例外是如果你在调用QCoreApplication::exec() main()之前在main()中这样做,因为该函数是阻塞的,并且在应用程序关闭之前不会返回。

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.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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