329f9c5b23e472ff3469bff52c2bae5eb2da909b
[profile/ivi/qtdeclarative.git] / doc / src / declarative / qtdeclarative.qdoc
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the documentation of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:FDL$
10 ** GNU Free Documentation License
11 ** Alternatively, this file may be used under the terms of the GNU Free
12 ** Documentation License version 1.3 as published by the Free Software
13 ** Foundation and appearing in the file included in the packaging of
14 ** this file.
15 **
16 ** Other Usage
17 ** Alternatively, this file may be used in accordance with the terms
18 ** and conditions contained in a signed written agreement between you
19 ** and Nokia.
20 **
21 **
22 **
23 **
24 ** $QT_END_LICENSE$
25 **
26 ****************************************************************************/
27
28 /*!
29   \module QtDeclarative
30   \title Qt Declarative Module
31   \ingroup modules
32
33   \brief The Qt Declarative module provides a declarative framework
34   for building highly dynamic, custom user interfaces.
35
36   To include the definitions of the module's classes, use the
37   following directive:
38
39   \code
40   #include <QtDeclarative>
41   \endcode
42
43   To link against the module, add this line to your \l qmake \c
44   .pro file:
45
46   \code
47   QT += declarative
48   \endcode
49
50   For more information on the Qt Declarative module, see the
51   \l{Qt Quick} documentation.
52 */
53
54
55 /*!
56   \macro QML_DECLARE_TYPE()
57   \relates QDeclarativeEngine
58
59   Equivalent to \c Q_DECLARE_METATYPE(TYPE *) and \c Q_DECLARE_METATYPE(QDeclarativeListProperty<TYPE>)
60
61   #include <QtDeclarative> to use this macro.
62 */
63
64 /*!
65   \macro QML_DECLARE_TYPEINFO(Type,Flags)
66   \relates QDeclarativeEngine
67
68   Declares additional properties of the given \a Type as described by the
69   specified \a Flags.
70   
71   Current the only supported type info is \c QML_HAS_ATTACHED_PROPERTIES which
72   declares that the \a Type supports \l {Attached Properties}.
73
74   #include <QtDeclarative> to use this macro.
75 */
76
77
78 /*!
79   \fn int qmlRegisterType(const char *uri, int versionMajor, int versionMinor, const char *qmlName)
80   \relates QDeclarativeEngine
81
82   This template function registers the C++ type in the QML system with
83   the name \a qmlName, in the library imported from \a uri having the
84   version number composed from \a versionMajor and \a versionMinor.
85
86   Returns the QML type id.
87
88   There are two forms of this template function:
89
90   \code
91   template<typename T>
92   int qmlRegisterType(const char *uri, int versionMajor, int versionMinor, const char *qmlName);
93
94   template<typename T, int metaObjectRevision>
95   int qmlRegisterType(const char *uri, int versionMajor, int versionMinor, const char *qmlName);
96   \endcode
97
98   The former is the standard form which registers the type \i T as a new type.
99   The latter allows a particular revision of a class to be registered in
100   a specified version (see \l {QML Type Versioning}).
101
102
103   For example, this registers a C++ class \c MySliderItem as a QML type
104   named \c Slider for version 1.0 of a \l{QML Modules}{module} called
105   "com.mycompany.qmlcomponents":
106
107   \code
108   #include <QtDeclarative>
109
110   ...
111
112   qmlRegisterType<MySliderItem>("com.mycompany.qmlcomponents", 1, 0, "Slider");
113   \endcode
114
115   Once this is registered, the type can be used in QML by importing the 
116   specified module name and version number:
117
118   \qml
119   import com.mycompany.qmlcomponents 1.0
120
121   Slider {
122       // ...
123   }
124   \endqml
125
126   Note that it's perfectly reasonable for a library to register types to older versions
127   than the actual version of the library. Indeed, it is normal for the new library to allow
128   QML written to previous versions to continue to work, even if more advanced versions of
129   some of its types are available.
130 */
131
132 /*!
133   \fn int qmlRegisterRevision(const char *uri, int versionMajor, int versionMinor)
134   \relates QDeclarativeEngine
135
136   This template function registers the specified revision of a C++ type in the QML system with
137   the library imported from \a uri having the version number composed
138   from \a versionMajor and \a versionMinor.
139
140   Returns the QML type id.
141
142   \code
143   template<typename T, int metaObjectRevision>
144   int qmlRegisterRevision(const char *uri, int versionMajor, int versionMinor);
145   \endcode
146
147   This function is typically used to register the revision of a base class to
148   use for the specified module version (see \l {QML Type Versioning}).
149 */
150
151 /*!
152   \fn int qmlRegisterUncreatableType(const char *uri, int versionMajor, int versionMinor, const char *qmlName, const QString& message)
153   \relates QDeclarativeEngine
154
155   This template function registers the C++ type in the QML system with
156   the name \a qmlName, in the library imported from \a uri having the
157   version number composed from \a versionMajor and \a versionMinor.
158
159   While the type has a name and a type, it cannot be created, and the
160   given error \a message will result if creation is attempted.
161
162   This is useful where the type is only intended for providing attached properties or enum values.
163
164   Returns the QML type id.
165
166   #include <QtDeclarative> to use this function.
167
168   \sa qmlRegisterTypeNotAvailable()
169 */
170
171 /*!
172   \fn int qmlRegisterTypeNotAvailable(const char *uri, int versionMajor, int versionMinor, const char *qmlName, const QString& message)
173   \relates QDeclarativeEngine
174
175   This function registers a type in the QML system with the name \a qmlName, in the library imported from \a uri having the
176   version number composed from \a versionMajor and \a versionMinor, but any attempt to instantiate the type
177   will produce the given error \a message.
178
179   Normally, the types exported by a module should be fixed. However, if a C++ type is not available, you should
180   at least "reserve" the QML type name, and give the user of your module a meaningful error message.
181
182   Returns the QML type id.
183
184   Example:
185
186   \code
187   #ifdef NO_GAMES_ALLOWED
188   qmlRegisterTypeNotAvailable("MinehuntCore", 0, 1, "Game", "Get back to work, slacker!");
189   #else
190   qmlRegisterType<MinehuntGame>("MinehuntCore", 0, 1, "Game");
191   #endif
192   \endcode
193
194   This will cause any QML which uses this module and attempts to use the type to produce an error message:
195   \code
196   fun.qml: Get back to work, slacker!
197      Game {
198      ^
199   \endcode
200
201   Without this, a generic "Game is not a type" message would be given.
202
203   #include <QtDeclarative> to use this function.
204
205   \sa qmlRegisterUncreatableType()
206 */
207
208 /*!
209   \fn int qmlRegisterType()
210   \relates QDeclarativeEngine
211   \overload
212
213   This template function registers the C++ type in the QML
214   system. Instances of this type cannot be created from the QML
215   system.
216
217   #include <QtDeclarative> to use this function.
218
219   Returns the QML type id.
220 */
221
222 /*!
223   \fn int qmlRegisterInterface(const char *typeName)
224   \relates QDeclarativeEngine
225
226   This template function registers the C++ type in the QML system
227   under the name \a typeName.
228
229   #include <QtDeclarative> to use this function.
230
231   Returns the QML type id.
232 */
233
234 /*!
235    \fn int qmlRegisterModuleApi(const char *uri, int versionMajor, int versionMinor, QJSValue (*callback)(QDeclarativeEngine *, QJSEngine *))
236    \relates QDeclarativeEngine
237
238    This function may be used to register a module API provider \a callback in a particular \a uri
239    with a version specified in \a versionMajor and \a versionMinor.
240
241    Installing a module API into a uri allows developers to provide arbitrary functionality
242    (methods and properties) in a namespace that doesn't necessarily contain elements.
243
244    A module API may be either a QObject or a QJSValue.  Only one module API provider
245    may be registered into any given namespace (combination of \a uri, \a versionMajor and \a versionMinor).
246    This function should be used to register a module API provider function which returns a QJSValue as a module API.
247
248    \bold{NOTE:} QJSValue module API properties will \bold{not} trigger binding re-evaluation if changed.
249
250    Usage:
251    \code
252    // first, define the module API provider function (callback).
253    static QJSValue *example_qjsvalue_module_api_provider(QDeclarativeEngine *engine, QJSEngine *scriptEngine)
254    {
255        Q_UNUSED(engine)
256
257        static int seedValue = 5;
258        QJSValue example = scriptEngine->newObject();
259        example.setProperty("someProperty", seedValue++);
260        return example;
261    }
262
263    // second, register the module API provider with QML by calling this function in an initialization function.
264    ...
265    qmlRegisterModuleApi("Qt.example.qjsvalueApi", 1, 0, example_qjsvalue_module_api_provider);
266    ...
267    \endcode
268
269    In order to use the registered module API in QML, you must import the module API.
270    \qml
271    import QtQuick 2.0
272    import Qt.example.qjsvalueApi 1.0 as ExampleApi
273    Item {
274        id: root
275        property int someValue: ExampleApi.someProperty
276    }
277    \endqml
278   */
279
280 /*!
281    \fn int qmlRegisterModuleApi(const char *uri, int versionMajor, int versionMinor, QObject *(*callback)(QDeclarativeEngine *, QJSEngine *))
282    \relates QDeclarativeEngine
283
284    This function may be used to register a module API provider \a callback in a particular \a uri
285    with a version specified in \a versionMajor and \a versionMinor.
286
287    Installing a module API into a uri allows developers to provide arbitrary functionality
288    (methods and properties) in a namespace that doesn't necessarily contain elements.
289
290    A module API may be either a QObject or a QJSValue.  Only one module API provider
291    may be registered into any given namespace (combination of \a uri, \a versionMajor and \a versionMinor).
292    This function should be used to register a module API provider function which returns a QObject as a module API.
293
294    A QObject module API must be imported with a qualifier, and that qualifier may be used as
295    the target in a \l Connections element or otherwise used as any other element id would.
296    One exception to this is that a QObject module API property may not be aliased (because the
297    module API qualifier does not identify an object within the same component as any other item).
298
299    Usage:
300    \code
301    // first, define your QObject which provides the functionality.
302    class ModuleApiExample : public QObject
303    {
304        Q_OBJECT
305        Q_PROPERTY (int someProperty READ someProperty WRITE setSomeProperty NOTIFY somePropertyChanged)
306
307    public:
308        ModuleApiExample(QObject* parent = 0)
309            : QObject(parent), m_someProperty(0)
310        {
311        }
312
313        ~ModuleApiExample() {}
314
315        Q_INVOKABLE int doSomething() { setSomeProperty(5); return m_someProperty; }
316
317        int someProperty() const { return m_someProperty; }
318        void setSomeProperty(int val) { m_someProperty = val; emit somePropertyChanged(val); }
319
320    signals:
321        void somePropertyChanged(int newValue);
322
323    private:
324        int m_someProperty;
325    };
326
327    // second, define the module API provider function (callback).
328    static QObject *example_qobject_module_api_provider(QDeclarativeEngine *engine, QJSEngine *scriptEngine)
329    {
330        Q_UNUSED(engine)
331        Q_UNUSED(scriptEngine)
332
333        ModuleApiExample *example = new ModuleApiExample();
334        return example;
335    }
336
337    // third, register the module API provider with QML by calling this function in an initialization function.
338    ...
339    qmlRegisterModuleApi("Qt.example.qobjectApi", 1, 0, example_qobject_module_api_provider);
340    ...
341    \endcode
342
343    In order to use the registered module API in QML, you must import the module API.
344    \qml
345    import QtQuick 2.0
346    import Qt.example.qobjectApi 1.0 as ExampleApi
347    Item {
348        id: root
349        property int someValue: ExampleApi.someProperty
350
351        Component.onCompleted: {
352            someValue = ExampleApi.doSomething()
353        }
354    }
355    \endqml
356   */