[Qt-qml] Temporary QML Objects

aaron.kennedy at nokia.com aaron.kennedy at nokia.com
Thu May 27 03:04:50 CEST 2010


Hi,

Objects returned to C++ like this are marked as "indestructible" to prevent them being destroyed under the nose of your C++ application.  You can change this by adding

QDeclarativeEngine::setObjectOwnership(myObject, QDeclarativeEngine::JavaScriptOwnership)

after your create() line if you like.

Cheers,

Aaron


On 27/05/10 10:01 AM, "ext Colin Kern" <colin.kern at gmail.com> wrote:

Hi all,

I want to dynamically create QML objects in my application to serve as
visual effects.  They're pretty basic QML components that I just want
to appear, animate, and then be destroyed.  I'm not sure about the
best way to do this.  My program is hybrid C++/QML, so I have a
QDeclarativeView which is the root context and engine for all the QML
of my program.  I create the view and then use setSource to set the
QML that makes up my main UI.  Now later as the program is running, I
want to start dynamically creating some objects.  This is what I tried
just as a proof of concept:

Effect.qml:
import Qt 4.6
Rectangle {
    id: rect
    width: 100
    height: 20
    color: "red"
    x: 200
    NumberAnimation on y { from: 300; to: 100; duration: 1000 }
    Component.onCompleted: rect.destroy(1000)
}

and then in a slot in my C++:
QDeclarativeComponent component(view->engine(), QUrl("Effect.qml"));
QObject *myObject = component.create();

this probably isn't the right way to go about this, but I'm not sure
what is.  When I run this code, I don't see the Effect elements, but I
do get debug messages saying "Effect.qml:10: Error: Invalid attempt
to destroy() an indestructible object".

Thanks for your help,
Colin
_______________________________________________
Qt-qml mailing list
Qt-qml at trolltech.com
http://lists.trolltech.com/mailman/listinfo/qt-qml

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt.nokia.com/pipermail/qt-qml/attachments/20100527/e0bf111c/attachment.html 


More information about the Qt-qml mailing list