简体   繁体   English

qsTr 在 QQuickView 中不起作用

[英]qsTr does not work in QQuickView

I use QQuickView to show qml interface in widget application我使用 QQuickView 在小部件应用程序中显示 qml 界面

  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:我需要使 qml 界面多语言,所以在实例化 QQuickView 之前,我为应用程序安装了新的翻译器:

  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. loadinstallTranslator函数都返回 true。 (Translations for target language exists in ts-file and compiled to qm-file which placed to right dir) (目标语言的翻译存在于 ts 文件中并编译为 qm 文件,放在正确的目录中)

Problem is that in C++ translations work well, following code ouputs string in target language问题是在 C++ 翻译中效果很好,下面的代码以目标语言输出字符串

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

but in qml interface diplayed by QQuickView string stays untraslated但是在 QQuickView 字符串显示的 qml 界面中保持未翻译

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.在调试时,我发现QCoreApplication::translate函数被 Qt 调用用于qsTr("translation test")并带有正确的sourceText变量,但是来自self->d_func()->translators所有 QTranslators 返回 null QStrings,因此可见文本保持未翻译状态。

Any thoughts why this happens and how to make text to be translated in QQuickView?任何想法为什么会发生这种情况以及如何在 QQuickView 中翻译文本?

I actually had that problem too C++ Translations would work, QML Translations would not (QT 5.12.8).我实际上也有这个问题 C++ 翻译可以工作,QML 翻译不会(QT 5.12.8)。 And it cost me maaaaaaany hours to find a solution.我花了好几个小时才找到解决方案。

For qml files I used a resource.qrc file.对于 qml 文件,我使用了一个 resource.qrc 文件。 And I named my QML-Files differently.我以不同的方式命名了我的 QML 文件。 So instead of status.qml I gave them an alias as statusQML.所以我给了他们一个别名作为 statusQML,而不是 status.qml。

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.我猜这可能是一个 QT-Bug。

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

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