Improve "Cannot assign object to list property" error message

Before:

    Cannot assign object to list property "animations"

After:

    Cannot assign object of type "QQuickFrameAnimation" to list property "animations"; expected "QQuickAbstractAnimation*"

Fixes: QTBUG-137469
Pick-to: 6.5 6.8 6.9 6.10
Change-Id: Iddf52d4262720236ffc610b6f7326bb126c53509
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
This commit is contained in:
Mitch Curtis 2025-06-09 16:14:11 +08:00
parent 5982907a45
commit 6e6cfe4f96
5 changed files with 24 additions and 5 deletions

View File

@ -732,7 +732,10 @@ QQmlError QQmlPropertyValidator::validateObjectBinding(const QQmlPropertyData *p
if (!QQmlMetaType::isInterface(listType)) {
QQmlPropertyCache::ConstPtr source = propertyCaches.at(binding->value.objectIndex);
if (!canCoerce(listType, source)) {
return qQmlCompileError(binding->valueLocation, tr("Cannot assign object to list property \"%1\"").arg(propertyName));
const QString expectedTypeName = QString::fromUtf8(listType.name()).remove(QLatin1Char('*'));
return qQmlCompileError(binding->valueLocation,
tr("Cannot assign object of type \"%1\" to list property \"%2\"; expected \"%3\"")
.arg(source->className(), propertyName, expectedTypeName));
}
}
return noError;

View File

@ -0,0 +1,5 @@
import QtQuick
SequentialAnimation {
FrameAnimation {}
}

View File

@ -1 +1 @@
4:15:Cannot assign object to list property "children"
4:15:Cannot assign object of type "QQmlComponent" to list property "children"; expected "QQuickItem"

View File

@ -1 +1 @@
4:24:Cannot assign object to list property "containerChildren"
4:24:Cannot assign object of type "QObject" to list property "containerChildren"; expected "MyContainer"

View File

@ -529,6 +529,8 @@ private slots:
void enumTypeAnnotations();
void assignWrongTypeToObjectList();
private:
QQmlEngine engine;
QStringList defaultImportPathList;
@ -8984,8 +8986,8 @@ void tst_qqmllanguage::overrideDefaultProperty()
QQmlComponent c(&e, url);
QVERIFY(c.isError());
QCOMPARE(c.errorString(),
url.toString() + QLatin1String(":5 Cannot assign object to list property \"data\"\n"));
QCOMPARE(c.errorString(), url.toString() + QLatin1String(
":5 Cannot assign object of type \"QQuickItem\" to list property \"data\"; expected \"QVariant\"\n"));
}
void tst_qqmllanguage::enumScopes()
@ -10036,6 +10038,15 @@ void tst_qqmllanguage::enumTypeAnnotations()
QVERIFY(!o.isNull());
}
void tst_qqmllanguage::assignWrongTypeToObjectList()
{
QQmlEngine engine;
QQmlComponent component(&engine, testFileUrl("assignWrongTypeToObjectList.qml"));
QVERIFY(!component.isReady());
QVERIFY(QRegularExpression(".*Cannot assign object of type \"QQuickFrameAnimation\""_L1
+ " to list property \"animations\"; expected \"QQuickAbstractAnimation\""_L1).match(component.errorString()).hasMatch());
}
QTEST_MAIN(tst_qqmllanguage)
#include "tst_qqmllanguage.moc"