[英]qsTr does not work in QQuickView
我使用 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);
load和installTranslator函數都返回 true。 (目標語言的翻譯存在於 ts 文件中並編譯為 qm 文件,放在正確的目錄中)
問題是在 C++ 翻譯中效果很好,下面的代碼以目標語言輸出字符串
qDebug() << tr("translation test");
但是在 QQuickView 字符串顯示的 qml 界面中保持未翻譯
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 中翻譯文本?
我實際上也有這個問題 C++ 翻譯可以工作,QML 翻譯不會(QT 5.12.8)。 我花了好幾個小時才找到解決方案。
對於 qml 文件,我使用了一個 resource.qrc 文件。 我以不同的方式命名了我的 QML 文件。 所以我給了他們一個別名作為 statusQML,而不是 status.qml。
this->pStatus->load(QUrl(QLatin1String("qrc:/statusQML")));
這沒有用。 解決方案:去掉資源文件中的別名並像這樣打開:
this->pStatus->load(QUrl(QLatin1String("qrc:/status.qml")));
瞧。
希望這可以讓某人節省一些時間。 我猜這可能是一個 QT-Bug。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.