我使用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);我需要使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);installTranslator函数和load函数都返回true。(目标语言的翻译存在于ts-file中,并被编译为qm-file,该文件位于右dir)
问题在于,在C++翻译中,下面的代码输出了目标语言中的字符串。
qDebug() << tr("translation test");但是在qml接口中,QQuickView字符串所播放的字符串保持未转换。
Text {
id: title
text: qsTr("translation test")
font.pixelSize: 36
font.bold: true
anchors.horizontalCenter: parent.horizontalCenter
}在调试过程中,我发现QCoreApplication::translate函数是由Qt为qsTr("translation test")调用的,具有正确的sourceText变量,但是self->d_func()->translators中的所有QTranslators都返回null QStrings,因此可见文本保持未翻译。
有什么想法,为什么会发生这种情况,以及如何使文本被翻译成QQuickView?
发布于 2021-05-13 16:32:56
实际上,我也遇到了这个问题,C++翻译也会起作用,不会起作用(QT5.12.8)。我花了好几个小时才找到解决办法。
对于qml文件,我使用了一个resource.qrc文件。我给我的QML文件取了不同的名字。所以,我给了他们一个别名statusQML,而不是statusQML。
this->pStatus->load(QUrl(QLatin1String("qrc:/statusQML")));这是行不通的。解决方案:删除资源文件中的Alias,然后这样打开:
this->pStatus->load(QUrl(QLatin1String("qrc:/status.qml")));哇哦。
希望这能给别人留几个小时。我想这可能是个QT-Bug。
https://stackoverflow.com/questions/44182077
复制相似问题