How to get translation to work outside the class?

前端 未结 1 1153
囚心锁ツ
囚心锁ツ 2021-01-12 05:09

I\'m writing a API to be used in the creation of Interfaces at work. The API allows the user to pick from a set of pre-built widgets and layouts so that multiple interfaces

相关标签:
1条回答
  • 2021-01-12 05:39

    I suspect this is due to the fact that:

    m_pButton = new MyWidget(tr("TEST"));
    

    defines a string in the context of MainWindow, and you try to translate TEST in the context of MyWidget. You can circumvent this by using the static tr() method on QObject. This will define TEST as a translation in the global QObject context. This can be done by adapting the button widget creation code as follows:

    m_pButton = new MyWidget(QObject::tr("TEST"));
    

    and in MyWidget::retranslate():

    setText(QObject::tr(name().toStdString().c_str()));
    

    Note that you need to regenerate your translation files for this to work!

    0 讨论(0)
提交回复
热议问题