diff options
author | Alan Alpert <alan.alpert@nokia.com> | 2010-04-19 11:42:49 (GMT) |
---|---|---|
committer | Alan Alpert <alan.alpert@nokia.com> | 2010-04-21 07:13:51 (GMT) |
commit | 202af3021362d3c8066bc479a95e7aad889bd928 (patch) | |
tree | 03587443c77403321c18e384b3277abd576e9ba3 /src/declarative | |
parent | 2ac290f56c6ad4f3e236034a7b99ecdb598ecc3a (diff) | |
download | Qt-202af3021362d3c8066bc479a95e7aad889bd928.zip Qt-202af3021362d3c8066bc479a95e7aad889bd928.tar.gz Qt-202af3021362d3c8066bc479a95e7aad889bd928.tar.bz2 |
Make the dynamic creation functions on the Qt object
Also updated examples to still work, and the dynamic example now uses
exceptions a little, to demonstrate that it can be done. Exceptions are
also now filled out with the QML error data.
Task-number: QT-2801
Reviewed-by: Aaron Kennedy
Diffstat (limited to 'src/declarative')
-rw-r--r-- | src/declarative/QmlChanges.txt | 2 | ||||
-rw-r--r-- | src/declarative/qml/qdeclarativeengine.cpp | 50 |
2 files changed, 38 insertions, 14 deletions
diff --git a/src/declarative/QmlChanges.txt b/src/declarative/QmlChanges.txt index d5910e3..2cae293 100644 --- a/src/declarative/QmlChanges.txt +++ b/src/declarative/QmlChanges.txt @@ -19,6 +19,8 @@ Animation: replace repeat with loops (loops: Animation.Infinite gives the old re AnchorChanges: use natural form to specify anchors (anchors.left instead of left) AnchorChanges: removed reset property. (reset: "left" should now be anchors.left: undefined) PathView: snapPosition replaced by preferredHighlightBegin, preferredHighlightEnd +createQmlObject: Moved to the Qt object, now use Qt.createQmlObject() +createComponent: Moved to the Qt object, now use Qt.createComponent() C++ API ------- diff --git a/src/declarative/qml/qdeclarativeengine.cpp b/src/declarative/qml/qdeclarativeengine.cpp index 9cd6b3c..53d830b 100644 --- a/src/declarative/qml/qdeclarativeengine.cpp +++ b/src/declarative/qml/qdeclarativeengine.cpp @@ -254,19 +254,19 @@ QDeclarativeScriptEngine::QDeclarativeScriptEngine(QDeclarativeEnginePrivate *pr qtObject.setProperty(QLatin1String("quit"), newFunction(QDeclarativeEnginePrivate::quit, 0)); qtObject.setProperty(QLatin1String("resolvedUrl"),newFunction(QDeclarativeScriptEngine::resolvedUrl, 1)); + if (mainthread) { + qtObject.setProperty(QLatin1String("createQmlObject"), + newFunction(QDeclarativeEnginePrivate::createQmlObject, 1)); + qtObject.setProperty(QLatin1String("createComponent"), + newFunction(QDeclarativeEnginePrivate::createComponent, 1)); + } + //firebug/webkit compat QScriptValue consoleObject = newObject(); consoleObject.setProperty(QLatin1String("log"),newFunction(QDeclarativeEnginePrivate::consoleLog, 1)); consoleObject.setProperty(QLatin1String("debug"),newFunction(QDeclarativeEnginePrivate::consoleLog, 1)); globalObject().setProperty(QLatin1String("console"), consoleObject); - if (mainthread) { - globalObject().setProperty(QLatin1String("createQmlObject"), - newFunction(QDeclarativeEnginePrivate::createQmlObject, 1)); - globalObject().setProperty(QLatin1String("createComponent"), - newFunction(QDeclarativeEnginePrivate::createComponent, 1)); - } - // translation functions need to be installed // before the global script class is constructed (QTBUG-6437) installTranslatorFunctions(); @@ -1019,7 +1019,7 @@ QScriptValue QDeclarativeEnginePrivate::createQmlObject(QScriptContext *ctxt, QS url = context->resolvedUrl(url); QObject *parentArg = activeEnginePriv->objectClass->toQObject(ctxt->argument(1)); - if(!parentArg) + if(!parentArg) return ctxt->throwError(QDeclarativeEngine::tr("parent object not found")); QDeclarativeComponent component(activeEngine); @@ -1027,10 +1027,21 @@ QScriptValue QDeclarativeEnginePrivate::createQmlObject(QScriptContext *ctxt, QS if(component.isError()) { QList<QDeclarativeError> errors = component.errors(); - QString errstr = QLatin1String("Qt.createQmlObject(): "); - foreach (const QDeclarativeError &error, errors) + QString errstr = QLatin1String("Qt.createQmlObject() failed to create object: "); + QScriptValue arr = ctxt->engine()->newArray(errors.length()); + int i = 0; + foreach (const QDeclarativeError &error, errors){ errstr += QLatin1String(" ") + error.toString() + QLatin1String("\n"); - return ctxt->throwError(errstr); + QScriptValue qmlErrObject = ctxt->engine()->newObject(); + qmlErrObject.setProperty("lineNumber", QScriptValue(error.line())); + qmlErrObject.setProperty("columnNumber", QScriptValue(error.column())); + qmlErrObject.setProperty("fileName", QScriptValue(error.url())); + qmlErrObject.setProperty("message", QScriptValue(error.description())); + arr.setProperty(i++, qmlErrObject); + } + QScriptValue err = ctxt->throwError(errstr); + err.setProperty("qmlErrors",arr); + return err; } if (!component.isReady()) @@ -1040,10 +1051,21 @@ QScriptValue QDeclarativeEnginePrivate::createQmlObject(QScriptContext *ctxt, QS if(component.isError()) { QList<QDeclarativeError> errors = component.errors(); - QString errstr = QLatin1String("Qt.createQmlObject(): "); - foreach (const QDeclarativeError &error, errors) + QString errstr = QLatin1String("Qt.createQmlObject() failed to create object: "); + QScriptValue arr = ctxt->engine()->newArray(errors.length()); + int i = 0; + foreach (const QDeclarativeError &error, errors){ errstr += QLatin1String(" ") + error.toString() + QLatin1String("\n"); - return ctxt->throwError(errstr); + QScriptValue qmlErrObject = ctxt->engine()->newObject(); + qmlErrObject.setProperty("lineNumber", QScriptValue(error.line())); + qmlErrObject.setProperty("columnNumber", QScriptValue(error.column())); + qmlErrObject.setProperty("fileName", QScriptValue(error.url())); + qmlErrObject.setProperty("message", QScriptValue(error.description())); + arr.setProperty(i++, qmlErrObject); + } + QScriptValue err = ctxt->throwError(errstr); + err.setProperty("qmlErrors",arr); + return err; } Q_ASSERT(obj); |