aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/qml/v8
diff options
context:
space:
mode:
authorMatthew Vogt <matthew.vogt@nokia.com>2012-03-19 15:56:22 +1000
committerQt by Nokia <qt-info@nokia.com>2012-03-20 02:58:26 +0100
commit610df5cdf87b9e1566b01a273fe67905b035cb93 (patch)
tree8d9226e18eef78830b70a1f0edf3b8054cd3b1f4 /src/qml/qml/v8
parentc5cd60a96d19a89e466f0ad62c2f5a625aaf4d8c (diff)
Allow parent to be specified for Qt.createComponent
Add an optional parameter to the Qt.createComponent function which allows the caller to specify the object that will become the parent for the created component. If the parent is not specified by the caller, it becomes the QML engine; this behavior will change in the near future to allow these component objects to be collected when no longer referenced. Task-number: QTBUG-24840 Change-Id: I2742133fe8ab8cbc80d8012a9d9b9fc2e4596fca Reviewed-by: Martin Jones <martin.jones@nokia.com>
Diffstat (limited to 'src/qml/qml/v8')
-rw-r--r--src/qml/qml/v8/qqmlbuiltinfunctions.cpp37
1 files changed, 32 insertions, 5 deletions
diff --git a/src/qml/qml/v8/qqmlbuiltinfunctions.cpp b/src/qml/qml/v8/qqmlbuiltinfunctions.cpp
index b9f2b627da..57a1cb0dcb 100644
--- a/src/qml/qml/v8/qqmlbuiltinfunctions.cpp
+++ b/src/qml/qml/v8/qqmlbuiltinfunctions.cpp
@@ -1120,7 +1120,7 @@ v8::Handle<v8::Value> createQmlObject(const v8::Arguments &args)
}
/*!
-\qmlmethod object Qt::createComponent(url, mode)
+\qmlmethod object Qt::createComponent(url, mode, parent)
Returns a \l Component object created using the QML file at the specified \a url,
or \c null if an empty string was given.
@@ -1135,6 +1135,9 @@ will be \c Component.Loading while it is loading. The status will change to
\c Component.Ready if the component loads successfully, or \c Component.Error
if loading fails.
+If the optional \a parent parameter is given, it should refer to the object
+that will become the parent for the created \l Component object.
+
Call \l {Component::createObject()}{Component.createObject()} on the returned
component to create an object instance of the component.
@@ -1150,7 +1153,8 @@ use \l{QML:Qt::createQmlObject()}{Qt.createQmlObject()}.
v8::Handle<v8::Value> createComponent(const v8::Arguments &args)
{
const char *invalidArgs = "Qt.createComponent(): Invalid arguments";
- if (args.Length() < 1 || args.Length() > 2)
+ const char *invalidParent = "Qt.createComponent(): Invalid parent object";
+ if (args.Length() < 1 || args.Length() > 3)
V8THROW_ERROR(invalidArgs);
QV8Engine *v8engine = V8ENGINE();
@@ -1167,19 +1171,42 @@ v8::Handle<v8::Value> createComponent(const v8::Arguments &args)
return v8::Null();
QQmlComponent::CompilationMode compileMode = QQmlComponent::PreferSynchronous;
- if (args.Length() == 2) {
+
+ // Default to engine parent; this will be removed in the near future (QTBUG-24841)
+ QObject *parentArg = engine;
+
+ unsigned consumedCount = 1;
+ if (args.Length() > 1) {
+ const v8::Local<v8::Value> &lastArg = args[args.Length()-1];
+
+ // The second argument could be the mode enum
if (args[1]->IsInt32()) {
int mode = args[1]->Int32Value();
if (mode != int(QQmlComponent::PreferSynchronous) && mode != int(QQmlComponent::Asynchronous))
V8THROW_ERROR(invalidArgs);
compileMode = QQmlComponent::CompilationMode(mode);
+ consumedCount += 1;
} else {
- V8THROW_ERROR(invalidArgs);
+ // The second argument could be the parent only if there are exactly two args
+ if ((args.Length() != 2) || !(lastArg->IsObject() || lastArg->IsNull()))
+ V8THROW_ERROR(invalidArgs);
+ }
+
+ if (consumedCount < args.Length()) {
+ if (lastArg->IsObject()) {
+ parentArg = v8engine->toQObject(lastArg);
+ if (!parentArg)
+ V8THROW_ERROR(invalidParent);
+ } else if (lastArg->IsNull()) {
+ parentArg = 0;
+ } else {
+ V8THROW_ERROR(invalidParent);
+ }
}
}
QUrl url = context->resolvedUrl(QUrl(arg));
- QQmlComponent *c = new QQmlComponent(engine, url, compileMode, engine);
+ QQmlComponent *c = new QQmlComponent(engine, url, compileMode, parentArg);
QQmlComponentPrivate::get(c)->creationContext = effectiveContext;
QQmlData::get(c, true)->setImplicitDestructible();
return v8engine->newQObject(c);