diff options
author | Aaron Kennedy <aaron.kennedy@nokia.com> | 2009-06-09 05:57:17 (GMT) |
---|---|---|
committer | Aaron Kennedy <aaron.kennedy@nokia.com> | 2009-06-09 05:57:17 (GMT) |
commit | 1ef0d84c8e5d706e60e487fc801f90bbf5fddaa6 (patch) | |
tree | 0e630af6b2e38af7615f4e334ce2bb51d8856b36 /src/declarative/qml/qmlengine.cpp | |
parent | fd346fc1f24ca39385a9e0b4cb188f157eea2b55 (diff) | |
parent | 45ad5f8e643f8a1bd52143ad628e5418f5ae8fa3 (diff) | |
download | Qt-1ef0d84c8e5d706e60e487fc801f90bbf5fddaa6.zip Qt-1ef0d84c8e5d706e60e487fc801f90bbf5fddaa6.tar.gz Qt-1ef0d84c8e5d706e60e487fc801f90bbf5fddaa6.tar.bz2 |
Merge branch 'kinetic-declarativeui' of git@scm.dev.nokia.troll.no:qt/kinetic into kinetic-declarativeui
Diffstat (limited to 'src/declarative/qml/qmlengine.cpp')
-rw-r--r-- | src/declarative/qml/qmlengine.cpp | 74 |
1 files changed, 57 insertions, 17 deletions
diff --git a/src/declarative/qml/qmlengine.cpp b/src/declarative/qml/qmlengine.cpp index d8ca809..73b9245 100644 --- a/src/declarative/qml/qmlengine.cpp +++ b/src/declarative/qml/qmlengine.cpp @@ -842,27 +842,48 @@ QScriptValue QmlEngine::qmlScriptObject(QObject* object, QmlEngine* engine) This function takes the URL of a QML file as its only argument. It returns a component object which can be used to create and load that QML file. - Example JavaScript: + Example JavaScript is below, remember that QML files that might be loaded + over the network cannot be expected to be ready immediately. \code - component = createComponent("Sprite.qml"); - if(component.isReady()){ - sprite = component.create(); - if(sprite == 0){ + var component; + var sprite; + function finishCreation(){ + if(component.isReady()){ + sprite = component.createObject(); + if(sprite == 0){ + // Error Handling + }else{ + sprite.parent = page; + sprite.x = 200; + //... + } + }else if(component.isError()){ // Error Handling - }else{ - sprite.parent = page; - sprite.x = 200; - //... } + } + + component = createComponent("Sprite.qml"); + if(component.isReady()){ + finishCreation(); }else{ - // The finishCreation function does the same thing as the above - // if(component.isReady()) branch component.statusChanged.connect(finishCreation); } \endcode - Remember that QML files that might be loaded over the network cannot be - expected to be ready immediately. + If you are certain the files will be local, you could simplify to + + \code + component = createComponent("Sprite.qml"); + sprite = component.createObject(); + if(sprite == 0){ + // Error Handling + }else{ + sprite.parent = page; + sprite.x = 200; + //... + } + \endcode + */ QScriptValue QmlEngine::createComponent(QScriptContext *ctxt, QScriptEngine *engine) { @@ -872,8 +893,8 @@ QScriptValue QmlEngine::createComponent(QScriptContext *ctxt, QScriptEngine *eng if(ctxt->argumentCount() != 1 || !activeEngine){ c = new QmlComponent(activeEngine); }else{ - c = new QmlComponent(activeEngine, QUrl(ctxt->argument(0).toString()), - activeEngine); + QUrl url = QUrl(ctxt->argument(0).toString()); + c = new QmlComponent(activeEngine, url, activeEngine); } return engine->newQObject(c); } @@ -897,7 +918,7 @@ QScriptValue QmlEngine::createQMLObject(QScriptContext *ctxt, QScriptEngine *eng if(ctxt->argumentCount() < 1){ qWarning() << "createQMLObject requires a string argument."; }else{ - qWarning() << "createQMLObject failed inexplicably."; + qWarning() << "createQMLObject cannot find engine."; } return engine->nullValue(); } @@ -1518,18 +1539,37 @@ void QmlContextScriptClass::setProperty(QScriptValue &object, ///////////////////////////////////////////////////////////// /* The QmlObjectScriptClass handles property access for QObjects - via QtScript. + via QtScript. It is also used to provide a more useful API in + QtScript for QML. */ + +QScriptValue QmlObjectDestroy(QScriptContext *context, QScriptEngine *engine) +{ + QObject* obj = context->thisObject().data().toQObject(); + if(obj) + delete obj; + context->thisObject().setData(QScriptValue(engine, 0)); + return engine->nullValue(); +} + QmlObjectScriptClass::QmlObjectScriptClass(QmlEngine *bindEngine) : QmlScriptClass(bindEngine) { engine = bindEngine; + prototypeObject = engine->scriptEngine()->newObject(); + prototypeObject.setProperty("destroy", + engine->scriptEngine()->newFunction(QmlObjectDestroy)); } QmlObjectScriptClass::~QmlObjectScriptClass() { } +QScriptValue QmlObjectScriptClass::prototype() const +{ + return prototypeObject; +} + QScriptClass::QueryFlags QmlObjectScriptClass::queryProperty(const QScriptValue &object, const QScriptString &name, QueryFlags flags, uint *id) |