1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the documentation of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:FDL$
9 ** GNU Free Documentation License
10 ** Alternatively, this file may be used under the terms of the GNU Free
11 ** Documentation License version 1.3 as published by the Free Software
12 ** Foundation and appearing in the file included in the packaging of
16 ** Alternatively, this file may be used in accordance with the terms
17 ** and conditions contained in a signed written agreement between you
26 ****************************************************************************/
30 \title Qt Declarative Module
33 \brief The Qt Declarative module provides a declarative framework
34 for building highly dynamic, custom user interfaces.
36 To include the definitions of the module's classes, use the
40 #include <QtDeclarative>
43 To link against the module, add this line to your \l qmake \c
50 For more information on the Qt Declarative module, see the
51 \l{Qt Quick} documentation.
56 \macro QML_DECLARE_TYPE()
57 \relates QDeclarativeEngine
59 Equivalent to \c Q_DECLARE_METATYPE(TYPE *) and \c Q_DECLARE_METATYPE(QDeclarativeListProperty<TYPE>)
61 #include <QtDeclarative> to use this macro.
65 \macro QML_DECLARE_TYPEINFO(Type,Flags)
66 \relates QDeclarativeEngine
68 Declares additional properties of the given \a Type as described by the
71 Current the only supported type info is \c QML_HAS_ATTACHED_PROPERTIES which
72 declares that the \a Type supports \l {Attached Properties}.
74 #include <QtDeclarative> to use this macro.
79 \fn int qmlRegisterType(const char *uri, int versionMajor, int versionMinor, const char *qmlName)
80 \relates QDeclarativeEngine
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.
86 Returns the QML type id.
88 There are two forms of this template function:
92 int qmlRegisterType(const char *uri, int versionMajor, int versionMinor, const char *qmlName);
94 template<typename T, int metaObjectRevision>
95 int qmlRegisterType(const char *uri, int versionMajor, int versionMinor, const char *qmlName);
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}).
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":
108 #include <QtDeclarative>
112 qmlRegisterType<MySliderItem>("com.mycompany.qmlcomponents", 1, 0, "Slider");
115 Once this is registered, the type can be used in QML by importing the
116 specified module name and version number:
119 import com.mycompany.qmlcomponents 1.0
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.
133 \fn int qmlRegisterRevision(const char *uri, int versionMajor, int versionMinor)
134 \relates QDeclarativeEngine
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.
140 Returns the QML type id.
143 template<typename T, int metaObjectRevision>
144 int qmlRegisterRevision(const char *uri, int versionMajor, int versionMinor);
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}).
152 \fn int qmlRegisterUncreatableType(const char *uri, int versionMajor, int versionMinor, const char *qmlName, const QString& message)
153 \relates QDeclarativeEngine
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.
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.
162 This is useful where the type is only intended for providing attached properties or enum values.
164 Returns the QML type id.
166 #include <QtDeclarative> to use this function.
168 \sa qmlRegisterTypeNotAvailable()
172 \fn int qmlRegisterTypeNotAvailable(const char *uri, int versionMajor, int versionMinor, const char *qmlName, const QString& message)
173 \relates QDeclarativeEngine
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.
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.
182 Returns the QML type id.
187 #ifdef NO_GAMES_ALLOWED
188 qmlRegisterTypeNotAvailable("MinehuntCore", 0, 1, "Game", "Get back to work, slacker!");
190 qmlRegisterType<MinehuntGame>("MinehuntCore", 0, 1, "Game");
194 This will cause any QML which uses this module and attempts to use the type to produce an error message:
196 fun.qml: Get back to work, slacker!
201 Without this, a generic "Game is not a type" message would be given.
203 #include <QtDeclarative> to use this function.
205 \sa qmlRegisterUncreatableType()
209 \fn int qmlRegisterType()
210 \relates QDeclarativeEngine
213 This template function registers the C++ type in the QML
214 system. Instances of this type cannot be created from the QML
217 #include <QtDeclarative> to use this function.
219 Returns the QML type id.
223 \fn int qmlRegisterInterface(const char *typeName)
224 \relates QDeclarativeEngine
226 This template function registers the C++ type in the QML system
227 under the name \a typeName.
229 #include <QtDeclarative> to use this function.
231 Returns the QML type id.
235 \fn int qmlRegisterModuleApi(const char *uri, int versionMajor, int versionMinor, QJSValue (*callback)(QDeclarativeEngine *, QJSEngine *))
236 \relates QDeclarativeEngine
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.
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.
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.
248 \bold{NOTE:} QJSValue module API properties will \bold{not} trigger binding re-evaluation if changed.
252 // first, define the module API provider function (callback).
253 static QJSValue *example_qjsvalue_module_api_provider(QDeclarativeEngine *engine, QJSEngine *scriptEngine)
257 static int seedValue = 5;
258 QJSValue example = scriptEngine->newObject();
259 example.setProperty("someProperty", seedValue++);
263 // second, register the module API provider with QML by calling this function in an initialization function.
265 qmlRegisterModuleApi("Qt.example.qjsvalueApi", 1, 0, example_qjsvalue_module_api_provider);
269 In order to use the registered module API in QML, you must import the module API.
272 import Qt.example.qjsvalueApi 1.0 as ExampleApi
275 property int someValue: ExampleApi.someProperty
281 \fn int qmlRegisterModuleApi(const char *uri, int versionMajor, int versionMinor, QObject *(*callback)(QDeclarativeEngine *, QJSEngine *))
282 \relates QDeclarativeEngine
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.
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.
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.
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).
301 // first, define your QObject which provides the functionality.
302 class ModuleApiExample : public QObject
305 Q_PROPERTY (int someProperty READ someProperty WRITE setSomeProperty NOTIFY somePropertyChanged)
308 ModuleApiExample(QObject* parent = 0)
309 : QObject(parent), m_someProperty(0)
313 ~ModuleApiExample() {}
315 Q_INVOKABLE int doSomething() { setSomeProperty(5); return m_someProperty; }
317 int someProperty() const { return m_someProperty; }
318 void setSomeProperty(int val) { m_someProperty = val; emit somePropertyChanged(val); }
321 void somePropertyChanged(int newValue);
327 // second, define the module API provider function (callback).
328 static QObject *example_qobject_module_api_provider(QDeclarativeEngine *engine, QJSEngine *scriptEngine)
331 Q_UNUSED(scriptEngine)
333 ModuleApiExample *example = new ModuleApiExample();
337 // third, register the module API provider with QML by calling this function in an initialization function.
339 qmlRegisterModuleApi("Qt.example.qobjectApi", 1, 0, example_qobject_module_api_provider);
343 In order to use the registered module API in QML, you must import the module API.
346 import Qt.example.qobjectApi 1.0 as ExampleApi
349 property int someValue: ExampleApi.someProperty
351 Component.onCompleted: {
352 someValue = ExampleApi.doSomething()