source: trunk/doc/src/declarative/dynamicobjects.qdoc@ 846

Last change on this file since 846 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 8.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the documentation of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:FDL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in a
14** written agreement between you and Nokia.
15**
16** GNU Free Documentation License
17** Alternatively, this file may be used under the terms of the GNU Free
18** Documentation License version 1.3 as published by the Free Software
19** Foundation and appearing in the file included in the packaging of this
20** file.
21**
22** If you have questions regarding the use of this file, please contact
23** Nokia at [email protected].
24** $QT_END_LICENSE$
25**
26****************************************************************************/
27
28/*!
29\page qdeclarativedynamicobjects.html
30\title Dynamic Object Management in QML
31
32QML provides a number of ways to dynamically create and manage QML objects.
33The \l{Loader}, \l{Repeater}, \l{ListView}, \l{GridView} and \l{PathView} elements
34all support dynamic object management. Objects can also be created and managed
35from C++, and this is the preferred method for hybrid QML/C++ applications
36(see \l{Using QML in C++ Applications}).
37
38QML also supports the dynamic creation of objects from within JavaScript
39code. This is useful if the existing QML elements do not fit the needs of your
40application, and there are no C++ components involved.
41
42See the \l {declarative/toys/dynamicscene}{Dynamic Scene example} for a demonstration
43of the concepts discussed on this page.
44
45
46\section1 Creating Objects Dynamically
47
48There are two ways to create objects dynamically from JavaScript. You can either call
49\l {QML:Qt::createComponent()}{Qt.createComponent()} to dynamically create
50a \l Component object, or use \l{QML:Qt::createQmlObject()}{Qt.createQmlObject()}
51to create an item from a string of QML.
52Creating a component is better if you have an existing component defined in a \c .qml
53file, and you want to dynamically create instances of that component. Otherwise,
54creating an item from a string of QML is useful when the item QML itself is generated
55at runtime.
56
57
58\section2 Creating a Component dynamically
59
60To dynamically load a component defined in a QML file, call the
61\l {QML:Qt::createComponent()}{Qt.createComponent()} function on the \l{QML Global Object}.
62This function takes the URL of the QML file as its only argument and creates
63a \l Component object from this URL.
64
65Once you have a \l Component, you can call its \l {Component::createObject()}{createObject()} method to create an instance of
66the component. This function takes exactly one argument, which is the parent for the new item. Since graphical items will
67not appear on the scene without a parent, it is recommended that you set the parent this way. However, if you wish to set
68the parent later you can safely pass \c null to this function.
69
70Here is an example. First there is \c Sprite.qml, which defines a simple QML component:
71
72\snippet doc/src/snippets/declarative/Sprite.qml 0
73
74Our main application file, \c main.qml, imports a \c componentCreation.js JavaScript file
75that will create \c Sprite objects:
76
77\snippet doc/src/snippets/declarative/createComponent.qml 0
78
79Here is \c componentCreation.js. Notice it checks whether the component \l{Component::status}{status} is
80\c Component.Ready before calling \l {Component::createObject()}{createObject()}
81in case the QML file is loaded over a network and thus is not ready immediately.
82
83\snippet doc/src/snippets/declarative/componentCreation.js vars
84\codeline
85\snippet doc/src/snippets/declarative/componentCreation.js func
86\snippet doc/src/snippets/declarative/componentCreation.js remote
87\snippet doc/src/snippets/declarative/componentCreation.js func-end
88\codeline
89\snippet doc/src/snippets/declarative/componentCreation.js finishCreation
90
91If you are certain the QML file to be loaded is a local file, you could omit the \c finishCreation()
92function and call \l {Component::createObject()}{createObject()} immediately:
93
94\snippet doc/src/snippets/declarative/componentCreation.js func
95\snippet doc/src/snippets/declarative/componentCreation.js local
96\snippet doc/src/snippets/declarative/componentCreation.js func-end
97
98Notice in both instances, \l {Component::createObject()}{createObject()} is called with
99\c appWindow passed as an argument so that the created object will become a child of the
100\c appWindow item in \c main.qml. Otherwise, the new item will not appear in the scene.
101
102When using files with relative paths, the path should
103be relative to the file where \l {QML:Qt::createComponent()}{Qt.createComponent()} is executed.
104
105To connect signals to (or receive signals from) dynamically created objects, use the signal
106\c connect() method. See \l {Connecting signals to methods and other signals} for more information.
107
108
109\section2 Creating an object from a string of QML
110
111If the QML is not defined until runtime, you can create a QML item from
112a string of QML using the \l{QML:Qt::createQmlObject()}{Qt.createQmlObject()} function, as in the following example:
113
114\snippet doc/src/snippets/declarative/createQmlObject.qml 0
115
116The first argument is the string of QML to create. Just like in a new file, you will need to
117import any types you wish to use. The second argument is the parent item for the new item;
118this should be an existing item in the scene. The third argument is the file path to associate
119with the new item; this is used for error reporting.
120
121If the string of QML imports files using relative paths, the path should be relative
122to the file in which the parent item (the second argument to the method) is defined.
123
124
125\section1 Maintaining Dynamically Created Objects
126
127When managing dynamically created items, you must ensure the creation context
128outlives the created item. Otherwise, if the creation context is destroyed first,
129the bindings in the dynamic item will no longer work.
130
131The actual creation context depends on how an item is created:
132
133\list
134\o If \l {QML:Qt::createComponent()}{Qt.createComponent()} is used, the creation context
135 is the QDeclarativeContext in which this method is called
136\o If \l{QML:Qt::createQmlObject()}{Qt.createQmlObject()}
137 if called, the creation context is the context of the parent item passed to this method
138\o If a \c {Component{}} item is defined and \l {Component::createObject()}{createObject()}
139 is called on that item, the creation context is the context in which the \c Component is defined
140\endlist
141
142Also, note that while dynamically created objects may be used the same as other objects, they
143do not have an id in QML.
144
145
146\section1 Deleting Objects Dynamically
147
148In many user interfaces, it is sufficient to set an item's opacity to 0 or
149to move the item off the screen instead of deleting the item. If you have
150lots of dynamically created items, however, you may receive a worthwhile
151performance benefit if unused items are deleted.
152
153Note that you should never manually delete items that were dynamically created
154by QML elements (such as \l Loader and \l Repeater). Also, you should avoid deleting
155items that you did not dynamically create yourself.
156
157Items can be deleted using the \c destroy() method. This method has an optional
158argument (which defaults to 0) that specifies the approximate delay in milliseconds
159before the object is to be destroyed.
160
161Here is an example. The \c application.qml creates five instances of the \c SelfDestroyingRect.qml
162component. Each instance runs a NumberAnimation, and when the animation has finished, calls
163\c destroy() on its root item to destroy itself:
164
165\table
166\row
167\o \c application.qml
168\o \c SelfDestroyingRect.qml
169
170\row
171\o \snippet doc/src/snippets/declarative/dynamicObjects-destroy.qml 0
172\o \snippet doc/src/snippets/declarative/SelfDestroyingRect.qml 0
173
174\endtable
175
176Alternatively, the \c application.qml could have destroyed the created object
177by calling \c object.destroy().
178
179Note that it is safe to call destroy() on an object within that object. Objects are not destroyed the
180instant destroy() is called, but are cleaned up sometime between the end of that script block and the next frame
181(unless you specified a non-zero delay).
182
183Note also that if a \c SelfDestroyingRect instance was created statically like this:
184
185\qml
186Item {
187 SelfDestroyingRect { ... }
188}
189\endqml
190
191This would result in an error, since items can only be dynamically
192destroyed if they were dynamically created.
193
194Objects created with \l{QML:Qt::createQmlObject()}{Qt.createQmlObject()}
195can similarly be destroyed using \c destroy():
196
197\snippet doc/src/snippets/declarative/createQmlObject.qml 0
198\snippet doc/src/snippets/declarative/createQmlObject.qml destroy
199
200*/
201
Note: See TracBrowser for help on using the repository browser.