From 4a8f38ed6ef6c1cb4a0ac7d2e123b6c42332fd07 Mon Sep 17 00:00:00 2001 From: Alan Alpert Date: Thu, 13 Aug 2009 12:31:54 +1000 Subject: Add delay parameter to the destroy function on QML objects Also updated dynamic example to have a fadeout effect on deletion. Also slipped in a toStr function, as it didn't work when I called it toString. --- examples/declarative/dynamic/DynRect.qml | 8 +++++++- examples/declarative/dynamic/dynamic.qml | 4 +++- src/declarative/qml/qmlengine.cpp | 34 +++++++++++++++++++++++++++++--- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/examples/declarative/dynamic/DynRect.qml b/examples/declarative/dynamic/DynRect.qml index 3f83afb..ec27441 100644 --- a/examples/declarative/dynamic/DynRect.qml +++ b/examples/declarative/dynamic/DynRect.qml @@ -1,3 +1,9 @@ import Qt 4.6 -Rect { color: "steelblue"; width: 100; height: 100; id: newRect } +Item { + states: State{ name: "dying"; SetProperties{ target: newRect; opacity: 0 } } + transitions: Transition{ + NumberAnimation{ properties: "opacity"; target: newRect; duration:500 } + } + Rect {color: "steelblue"; width: 100; height: 100; id: newRect } +} diff --git a/examples/declarative/dynamic/dynamic.qml b/examples/declarative/dynamic/dynamic.qml index 286e449..54d8dc8 100644 --- a/examples/declarative/dynamic/dynamic.qml +++ b/examples/declarative/dynamic/dynamic.qml @@ -19,10 +19,12 @@ Rect { id: page; width: 800; height: 800; color:"black" if(a!=null) { a.parent = targetItem2;//BUG: this should happen automatically fourthBox = a; + print(a.toStr()); extendStars = true; } } else { - fourthBox.destroy(); + fourthBox.state = 'dying'; + fourthBox.destroy(500); fourthBox = null; extendStars = false; } diff --git a/src/declarative/qml/qmlengine.cpp b/src/declarative/qml/qmlengine.cpp index bdbf2e7..5a10c3b 100644 --- a/src/declarative/qml/qmlengine.cpp +++ b/src/declarative/qml/qmlengine.cpp @@ -55,6 +55,7 @@ #include #include #include +#include #include #include #include @@ -929,12 +930,37 @@ void QmlValueTypeScriptClass::setProperty(QScriptValue &object, QtScript for QML. */ +QScriptValue QmlObjectToString(QScriptContext *context, QScriptEngine *engine) +{ + QObject* obj = context->thisObject().data().toQObject(); + QString ret = QLatin1String("Qml Object, "); + if(obj){ + //###Should this be designer or developer details? Dev for now. + //TODO: Can we print the id too? + ret += QLatin1String("\""); + ret += obj->objectName(); + ret += QLatin1String("\" "); + ret += obj->metaObject()->className(); + ret += QLatin1String("(0x"); + ret += QString::number((int)obj,16); + ret += QLatin1String(")"); + }else{ + ret += "null"; + } + return engine->newVariant(ret); +} + QScriptValue QmlObjectDestroy(QScriptContext *context, QScriptEngine *engine) { QObject* obj = context->thisObject().data().toQObject(); - if(obj) - delete obj; - context->thisObject().setData(QScriptValue(engine, 0)); + if(obj){ + int delay = 0; + if(context->argumentCount() > 0) + delay = context->argument(0).toInt32(); + QTimer::singleShot(delay, obj, SLOT(deleteLater())); + //### Should this be delayed as well? + context->thisObject().setData(QScriptValue(engine, 0)); + } return engine->nullValue(); } @@ -944,6 +970,8 @@ QmlObjectScriptClass::QmlObjectScriptClass(QmlEngine *bindEngine) engine = bindEngine; QScriptEngine *scriptEngine = QmlEnginePrivate::getScriptEngine(bindEngine); prototypeObject = scriptEngine->newObject(); + prototypeObject.setProperty(QLatin1String("toStr"),//TODO: Why won't toString work? + scriptEngine->newFunction(QmlObjectToString)); prototypeObject.setProperty(QLatin1String("destroy"), scriptEngine->newFunction(QmlObjectDestroy)); } -- cgit v0.12