[Qt-qml] Temporary QML Objects
Alan Alpert
alan.alpert at nokia.com
Thu May 27 13:41:19 CEST 2010
Torsdag 27. mai 2010 10:01:38 skrev ext Colin Kern :
> 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
You can also do this straight from QML. Note that if you dynamically create
the item in QML, it will automatically be destructible from QML. It is
probably easier and more flexible to do this from QML if you can (and given the
specific example above, you can do exactly that using only QML via the
Qt.createComponent function).
The Dynamic Scene example in examples/declarative/toys/dynamicscene does that
(and from QML). I would recommend that you have a look at that example if you
haven't already.
--
Alan Alpert
Software Engineer
Nokia, Qt Development Frameworks
More information about the Qt-qml
mailing list