QmlModels: Test model-writing behavior of delegates

When writing through a delegate, the original data is already written.
We are only missing change signals.

Task-number: QTBUG-139941
Change-Id: I3124a8dc5852e3858a1e9bd9b74c07608bb5e446
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
(cherry picked from commit b8b1b94cea)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Ulf Hermann 2025-09-05 12:49:51 +02:00 committed by Qt Cherry-pick Bot
parent 2f807fa229
commit c1fff61a4d
2 changed files with 33 additions and 0 deletions

View File

@ -63,6 +63,19 @@ DelegateModel {
property int y: 12
}
function xAt0() : real {
switch (modelIndex) {
case Model.Singular:
case Model.List:
return model.get(0).x
case Model.Array:
return model[0].x
case Model.Object:
return model.x
}
return -1;
}
property int modelIndex: Model.None
property int delegateIndex: Delegate.None

View File

@ -985,6 +985,18 @@ void tst_QQmlDelegateModel::delegateModelAccess()
? access != QQmlDelegateModel::ReadOnly
: access == QQmlDelegateModel::ReadWrite;
const bool writeShouldPropagate =
// If we've explicitly asked for the model to be written, it is
(access == QQmlDelegateModel::ReadWrite) ||
// If it's a QAIM or an object, it's implicitly written
(modelKind != Model::Kind::Array) ||
// When writing through the model object from a typed delegate,
// the value was propagated even before.
(access == QQmlDelegateModel::Qt5ReadWrite && delegateKind == Delegate::Typed);
double expected = 11;
QCOMPARE(delegate->property("immediateX").toDouble(), expected);
@ -997,6 +1009,10 @@ void tst_QQmlDelegateModel::delegateModelAccess()
QCOMPARE(delegate->property("immediateX").toDouble(), expected);
QCOMPARE(delegate->property("modelX").toDouble(), expected);
double xAt0 = -1;
QMetaObject::invokeMethod(object.data(), "xAt0", Q_RETURN_ARG(double, xAt0));
QCOMPARE(xAt0, writeShouldPropagate ? expected : 11);
if (immediateWritable)
expected = 1;
@ -1007,6 +1023,10 @@ void tst_QQmlDelegateModel::delegateModelAccess()
delegateKind == Delegate::Untyped ? expected : 1);
QCOMPARE(delegate->property("modelX").toDouble(), expected);
xAt0 = -1;
QMetaObject::invokeMethod(object.data(), "xAt0", Q_RETURN_ARG(double, xAt0));
QCOMPARE(xAt0, writeShouldPropagate ? expected : 11);
}
QTEST_MAIN(tst_QQmlDelegateModel)