From 45ca7aff2c04c302906a1af0d9d671bb9cb452f0 Mon Sep 17 00:00:00 2001 From: Aaron Kennedy Date: Fri, 9 Apr 2010 13:31:25 +1000 Subject: Use variant instead of var in QML In QML "var"s are not the same as JavaScript vars - they are QVariants instead. However, as they behave in a similar enough fashion to native JavaScript it can be confusing to developers when they are called "var". --- src/declarative/qml/qdeclarativebinding.cpp | 13 ------------- src/declarative/qml/qdeclarativeobjectscriptclass.cpp | 7 +------ src/declarative/qml/qdeclarativescriptparser.cpp | 11 +++++++++-- .../data/propertyAssignmentErrors.qml | 6 ------ .../qdeclarativeecmascript/tst_qdeclarativeecmascript.cpp | 4 +--- 5 files changed, 11 insertions(+), 30 deletions(-) diff --git a/src/declarative/qml/qdeclarativebinding.cpp b/src/declarative/qml/qdeclarativebinding.cpp index 6a99855..91eb915 100644 --- a/src/declarative/qml/qdeclarativebinding.cpp +++ b/src/declarative/qml/qdeclarativebinding.cpp @@ -170,19 +170,6 @@ void QDeclarativeBinding::update(QDeclarativePropertyPrivate::WriteFlags flags) if (data->error.isValid()) { - } else if (!scriptValue.isVariant() && value.userType() == QMetaType::QVariantList && - data->property.propertyType() == qMetaTypeId()) { - - // This case catches QtScript's automatic conversion to QVariantList for arrays - QUrl url = QUrl(data->url); - int line = data->line; - if (url.isEmpty()) url = QUrl(QLatin1String("")); - - data->error.setUrl(url); - data->error.setLine(line); - data->error.setColumn(-1); - data->error.setDescription(QLatin1String("Unable to assign JavaScript array to QML variant property")); - } else if (isUndefined && data->property.isResettable()) { data->property.reset(); diff --git a/src/declarative/qml/qdeclarativeobjectscriptclass.cpp b/src/declarative/qml/qdeclarativeobjectscriptclass.cpp index ec84da9..4601aaa 100644 --- a/src/declarative/qml/qdeclarativeobjectscriptclass.cpp +++ b/src/declarative/qml/qdeclarativeobjectscriptclass.cpp @@ -361,12 +361,7 @@ void QDeclarativeObjectScriptClass::setProperty(QObject *obj, else v = enginePriv->scriptValueToVariant(value, lastData->propType); - if (!value.isVariant() && v.userType() == QMetaType::QVariantList && - lastData->propType == qMetaTypeId()) { - - QString error = QLatin1String("Cannot assign JavaScript array to QML variant property"); - context->throwError(error); - } else if (!QDeclarativePropertyPrivate::write(obj, *lastData, v, evalContext)) { + if (!QDeclarativePropertyPrivate::write(obj, *lastData, v, evalContext)) { const char *valueType = 0; if (v.userType() == QVariant::Invalid) valueType = "null"; else valueType = QMetaType::typeName(v.userType()); diff --git a/src/declarative/qml/qdeclarativescriptparser.cpp b/src/declarative/qml/qdeclarativescriptparser.cpp index 507ff5b..92a10aa 100644 --- a/src/declarative/qml/qdeclarativescriptparser.cpp +++ b/src/declarative/qml/qdeclarativescriptparser.cpp @@ -535,7 +535,8 @@ bool ProcessAST::visit(AST::UiPublicMember *node) // { "time", Object::DynamicProperty::Time, "QTime" }, // { "date", Object::DynamicProperty::Date, "QDate" }, { "date", Object::DynamicProperty::DateTime, "QDateTime" }, - { "var", Object::DynamicProperty::Variant, "QVariant" } + { "var", Object::DynamicProperty::Variant, "QVariant" }, + { "variant", Object::DynamicProperty::Variant, "QVariant" } }; const int propTypeNameToTypesCount = sizeof(propTypeNameToTypes) / sizeof(propTypeNameToTypes[0]); @@ -563,7 +564,7 @@ bool ProcessAST::visit(AST::UiPublicMember *node) _parser->_errors << error; return false; } - + signal.parameterTypes << qtType; signal.parameterNames << p->name->asString().toUtf8(); p = p->finish(); @@ -646,6 +647,11 @@ bool ProcessAST::visit(AST::UiPublicMember *node) property.location = location(node->firstSourceLocation(), node->lastSourceLocation()); + if (memberType == QByteArray("var")) + qWarning().nospace() << qPrintable(_parser->_scriptFile) << ":" << property.location.start.line << ":" + << property.location.start.column << ": var type has been replaced by variant. " + << "Support will be removed entirely shortly."; + if (node->expression) { // default value property.defaultValue = new Property; property.defaultValue->parent = _stateStack.top().object; @@ -909,6 +915,7 @@ bool QDeclarativeScriptParser::parse(const QByteArray &qmldata, const QUrl &url) clear(); const QString fileName = url.toString(); + _scriptFile = fileName; QTextStream stream(qmldata, QIODevice::ReadOnly); stream.setCodec("UTF-8"); diff --git a/tests/auto/declarative/qdeclarativeecmascript/data/propertyAssignmentErrors.qml b/tests/auto/declarative/qdeclarativeecmascript/data/propertyAssignmentErrors.qml index 483179a..416dbce 100644 --- a/tests/auto/declarative/qdeclarativeecmascript/data/propertyAssignmentErrors.qml +++ b/tests/auto/declarative/qdeclarativeecmascript/data/propertyAssignmentErrors.qml @@ -14,12 +14,6 @@ QtObject { } try { - root.b = [ 10, root ] - } catch(e) { - console.log (e.fileName + ":" + e.lineNumber + ":" + e); - } - - try { root.a = "Hello"; } catch(e) { console.log (e.fileName + ":" + e.lineNumber + ":" + e); diff --git a/tests/auto/declarative/qdeclarativeecmascript/tst_qdeclarativeecmascript.cpp b/tests/auto/declarative/qdeclarativeecmascript/tst_qdeclarativeecmascript.cpp index 72f14f9..c6bb276 100644 --- a/tests/auto/declarative/qdeclarativeecmascript/tst_qdeclarativeecmascript.cpp +++ b/tests/auto/declarative/qdeclarativeecmascript/tst_qdeclarativeecmascript.cpp @@ -1026,12 +1026,10 @@ void tst_qdeclarativeecmascript::propertyAssignmentErrors() QString url = component.url().toString(); QString warning1 = url + ":11:Error: Cannot assign [undefined] to int"; - QString warning2 = url + ":17:Error: Cannot assign JavaScript array to QML variant property"; - QString warning3 = url + ":23:Error: Cannot assign QString to int"; + QString warning2 = url + ":17:Error: Cannot assign QString to int"; QTest::ignoreMessage(QtDebugMsg, warning1.toLatin1().constData()); QTest::ignoreMessage(QtDebugMsg, warning2.toLatin1().constData()); - QTest::ignoreMessage(QtDebugMsg, warning3.toLatin1().constData()); QObject *object = component.create(); QVERIFY(object != 0); -- cgit v0.12