qsTr does not work in QQuickView

765 views Asked by At

I use QQuickView to show qml interface in widget application

  m_window = new QQuickView();
  m_container = QWidget::createWindowContainer(m_window,hostWidget,Qt::FramelessWindowHint);
  m_container->setFocusPolicy(Qt::TabFocus);
  m_window->setResizeMode(QQuickView::SizeRootObjectToView);
  m_window->setSource(file_url);

I need to make qml interface multilingual, so before instantiating QQuickView i install new translator to application:

  m_qmlTranslator = new QTranslator(this); // this - host QWidget    
  m_qmlTranslator->load(QString::fromUtf8("translate_%1").arg(QLocale::system().name()),strTranslationDir);
  QScopedPointer<QCoreApplication> pAppl(QApplication::instance());
  pAppl->installTranslator(m_qmlTranslator);

both load and installTranslator functions returns true. (Translations for target language exists in ts-file and compiled to qm-file which placed to right dir)

Problem is that in C++ translations work well, following code ouputs string in target language

   qDebug() << tr("translation test");

but in qml interface diplayed by QQuickView string stays untraslated

Text {
    id: title
    text: qsTr("translation test")
    font.pixelSize: 36
    font.bold: true
    anchors.horizontalCenter: parent.horizontalCenter
}

while debugging i found out that QCoreApplication::translate function is called by Qt for qsTr("translation test") with correct sourceText variable, but all QTranslators from self->d_func()->translators returns null QStrings so visible text stays untranslated.

Any thoughts why this happens and how to make text to be translated in QQuickView?

1

There are 1 answers

0
TommySmoke On

I actually had that problem too C++ Translations would work, QML Translations would not (QT 5.12.8). And it cost me maaaaaaany hours to find a solution.

For qml files I used a resource.qrc file. And I named my QML-Files differently. So instead of status.qml I gave them an alias as statusQML.

this->pStatus->load(QUrl(QLatin1String("qrc:/statusQML")));

This did not work. The solution: Taking away the Alias in the resource-file and opening like that:

this->pStatus->load(QUrl(QLatin1String("qrc:/status.qml")));

Voilà.

Hopefully this will spare someone some hours. I guess this might be a QT-Bug.