Skip to content

Commit

Permalink
QQmlListModel: emit dataChange signal when updating the translations
Browse files Browse the repository at this point in the history
We can't really track the individual bindings. Updating all string
fields is a blunt thing to do, but it always works and it's not worse
than what we did before the translation binding optimization.

Pick-to: 6.2 6.4
Fixes: QTBUG-107208
Change-Id: I96b86c979b3168f052af4685874741f674e3337f
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
  • Loading branch information
Ulf Hermann committed Oct 11, 2022
1 parent d11460f commit 53fa870
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/qmlmodels/qqmllistmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2505,6 +2505,16 @@ void QQmlListModel::updateTranslations()
if (m_dynamicRoles)
return;
Q_ASSERT(m_listModel);

QList<int> roles;
for (int i = 0, end = m_listModel->roleCount(); i != end; ++i) {
if (m_listModel->getExistingRole(i).type == ListLayout::Role::String)
roles.append(i);
}

if (!roles.isEmpty())
emitItemsChanged(0, rowCount(QModelIndex()), roles);

m_listModel->updateTranslations();
}

Expand Down
1 change: 1 addition & 0 deletions tests/auto/qml/qqmltranslation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ qt_internal_add_test(tst_qqmltranslation
Qt::Gui
Qt::GuiPrivate
Qt::QmlPrivate
Qt::QmlModelsPrivate
Qt::Quick
Qt::QuickTestUtilsPrivate
TESTDATA ${test_data}
Expand Down
15 changes: 15 additions & 0 deletions tests/auto/qml/qqmltranslation/data/translatedElements.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import QtQml
import QtQml.Models

DelegateModel {
model: ListModel {
ListElement { dish: qsTr("soup"); price: 60 }
ListElement { dish: qsTr("fish"); price: 100 }
ListElement { dish: qsTr("meat"); price: 230 }
ListElement { dish: qsTr("bread"); price: 10 }
}

delegate: QtObject {
required property string dish
}
}
39 changes: 39 additions & 0 deletions tests/auto/qml/qqmltranslation/tst_qqmltranslation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <QQuickItem>
#include <private/qqmlengine_p.h>
#include <private/qqmltypedata_p.h>
#include <private/qqmldelegatemodel_p.h>
#include <QtQuickTestUtils/private/qmlutils_p.h>

class tst_qqmltranslation : public QQmlDataTest
Expand All @@ -23,6 +24,7 @@ private slots:
void idTranslation();
void translationChange();
void preferJSContext();
void listModel();
};

void tst_qqmltranslation::translation_data()
Expand Down Expand Up @@ -170,6 +172,14 @@ class DummyTranslator : public QTranslator
return QString::fromUtf8("Deutsch in mylibrary");
if (!qstrcmp(sourceText, "English in translation") && !qstrcmp(context, "nested_js_translation"))
return QString::fromUtf8("Deutsch in Setzung");
if (!qstrcmp(sourceText, "soup"))
return QString::fromUtf8("Suppe");
if (!qstrcmp(sourceText, "fish"))
return QString::fromUtf8("Fisch");
if (!qstrcmp(sourceText, "meat"))
return QString::fromUtf8("Fleisch");
if (!qstrcmp(sourceText, "bread"))
return QString::fromUtf8("Brot");
return QString();
}

Expand Down Expand Up @@ -231,6 +241,35 @@ void tst_qqmltranslation::preferJSContext()
QCoreApplication::removeTranslator(&translator);
}

void tst_qqmltranslation::listModel()
{
QQmlEngine engine;
QQmlComponent component(&engine, testFileUrl("translatedElements.qml"));
QVERIFY2(component.isReady(), qPrintable(component.errorString()));
QScopedPointer<QObject> o(component.create());
QVERIFY(o);

QQmlDelegateModel *model = qobject_cast<QQmlDelegateModel *>(o.data());
QVERIFY(model);

QCOMPARE(model->count(), 4);

QCOMPARE(model->object(0)->property("dish").toString(), QStringLiteral("soup"));
QCOMPARE(model->object(1)->property("dish").toString(), QStringLiteral("fish"));
QCOMPARE(model->object(2)->property("dish").toString(), QStringLiteral("meat"));
QCOMPARE(model->object(3)->property("dish").toString(), QStringLiteral("bread"));

DummyTranslator translator;
QCoreApplication::installTranslator(&translator);
engine.setUiLanguage(QStringLiteral("xxx"));
engine.retranslate();

QCOMPARE(model->object(0)->property("dish").toString(), QStringLiteral("Suppe"));
QCOMPARE(model->object(1)->property("dish").toString(), QStringLiteral("Fisch"));
QCOMPARE(model->object(2)->property("dish").toString(), QStringLiteral("Fleisch"));
QCOMPARE(model->object(3)->property("dish").toString(), QStringLiteral("Brot"));
}

QTEST_MAIN(tst_qqmltranslation)

#include "tst_qqmltranslation.moc"

0 comments on commit 53fa870

Please sign in to comment.