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 ****************************************************************************/
29 \macro QML_DECLARE_TYPE()
32 Equivalent to \c Q_DECLARE_METATYPE(TYPE *) and \c Q_DECLARE_METATYPE(QQmlListProperty<TYPE>)
34 #include <QtQml> to use this macro.
38 \macro QML_DECLARE_TYPEINFO(Type,Flags)
41 Declares additional properties of the given \a Type as described by the
44 Current the only supported type info is \c QML_HAS_ATTACHED_PROPERTIES which
45 declares that the \a Type supports \l {Attached Properties}.
47 #include <QtQml> to use this macro.
52 \fn int qmlRegisterType(const char *uri, int versionMajor, int versionMinor, const char *qmlName)
55 This template function registers the C++ type in the QML system with
56 the name \a qmlName, in the library imported from \a uri having the
57 version number composed from \a versionMajor and \a versionMinor.
59 Returns the QML type id.
61 There are two forms of this template function:
65 int qmlRegisterType(const char *uri, int versionMajor, int versionMinor, const char *qmlName);
67 template<typename T, int metaObjectRevision>
68 int qmlRegisterType(const char *uri, int versionMajor, int versionMinor, const char *qmlName);
71 The former is the standard form which registers the type \e T as a new type.
72 The latter allows a particular revision of a class to be registered in
73 a specified version (see \l {QML Type Versioning}).
76 For example, this registers a C++ class \c MySliderItem as a QML type
77 named \c Slider for version 1.0 of a \l{QML Modules}{module} called
78 "com.mycompany.qmlcomponents":
85 qmlRegisterType<MySliderItem>("com.mycompany.qmlcomponents", 1, 0, "Slider");
88 Once this is registered, the type can be used in QML by importing the
89 specified module name and version number:
92 import com.mycompany.qmlcomponents 1.0
99 Note that it's perfectly reasonable for a library to register types to older versions
100 than the actual version of the library. Indeed, it is normal for the new library to allow
101 QML written to previous versions to continue to work, even if more advanced versions of
102 some of its types are available.
106 \fn int qmlRegisterRevision(const char *uri, int versionMajor, int versionMinor)
109 This template function registers the specified revision of a C++ type in the QML system with
110 the library imported from \a uri having the version number composed
111 from \a versionMajor and \a versionMinor.
113 Returns the QML type id.
116 template<typename T, int metaObjectRevision>
117 int qmlRegisterRevision(const char *uri, int versionMajor, int versionMinor);
120 This function is typically used to register the revision of a base class to
121 use for the specified module version (see \l {QML Type Versioning}).
125 \fn int qmlRegisterUncreatableType(const char *uri, int versionMajor, int versionMinor, const char *qmlName, const QString& message)
128 This template function registers the C++ type in the QML system with
129 the name \a qmlName, in the library imported from \a uri having the
130 version number composed from \a versionMajor and \a versionMinor.
132 While the type has a name and a type, it cannot be created, and the
133 given error \a message will result if creation is attempted.
135 This is useful where the type is only intended for providing attached properties or enum values.
137 Returns the QML type id.
139 #include <QtQml> to use this function.
141 \sa qmlRegisterTypeNotAvailable()
145 \fn int qmlRegisterTypeNotAvailable(const char *uri, int versionMajor, int versionMinor, const char *qmlName, const QString& message)
148 This function registers a type in the QML system with the name \a qmlName, in the library imported from \a uri having the
149 version number composed from \a versionMajor and \a versionMinor, but any attempt to instantiate the type
150 will produce the given error \a message.
152 Normally, the types exported by a module should be fixed. However, if a C++ type is not available, you should
153 at least "reserve" the QML type name, and give the user of your module a meaningful error message.
155 Returns the QML type id.
160 #ifdef NO_GAMES_ALLOWED
161 qmlRegisterTypeNotAvailable("MinehuntCore", 0, 1, "Game", "Get back to work, slacker!");
163 qmlRegisterType<MinehuntGame>("MinehuntCore", 0, 1, "Game");
167 This will cause any QML which uses this module and attempts to use the type to produce an error message:
169 fun.qml: Get back to work, slacker!
174 Without this, a generic "Game is not a type" message would be given.
176 #include <QtQml> to use this function.
178 \sa qmlRegisterUncreatableType()
182 \fn int qmlRegisterType()
186 This template function registers the C++ type in the QML
187 system. Instances of this type cannot be created from the QML
190 #include <QtQml> to use this function.
192 Returns the QML type id.
196 \fn int qmlRegisterInterface(const char *typeName)
199 This template function registers the C++ type in the QML system
200 under the name \a typeName.
202 #include <QtQml> to use this function.
204 Returns the QML type id.
208 \fn int qmlRegisterModuleApi(const char *uri, int versionMajor, int versionMinor, QJSValue (*callback)(QQmlEngine *, QJSEngine *))
211 This function may be used to register a module API provider \a callback in a particular \a uri
212 with a version specified in \a versionMajor and \a versionMinor.
214 Installing a module API into a uri allows developers to provide arbitrary functionality
215 (methods and properties) in a namespace that doesn't necessarily contain elements.
217 A module API may be either a QObject or a QJSValue. Only one module API provider
218 may be registered into any given namespace (combination of \a uri, \a versionMajor and \a versionMinor).
219 This function should be used to register a module API provider function which returns a QJSValue as a module API.
221 \b{NOTE:} QJSValue module API properties will \b{not} trigger binding re-evaluation if changed.
225 // first, define the module API provider function (callback).
226 static QJSValue *example_qjsvalue_module_api_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
230 static int seedValue = 5;
231 QJSValue example = scriptEngine->newObject();
232 example.setProperty("someProperty", seedValue++);
236 // second, register the module API provider with QML by calling this function in an initialization function.
238 qmlRegisterModuleApi("Qt.example.qjsvalueApi", 1, 0, example_qjsvalue_module_api_provider);
242 In order to use the registered module API in QML, you must import the module API.
245 import Qt.example.qjsvalueApi 1.0 as ExampleApi
248 property int someValue: ExampleApi.someProperty
254 \fn template<typename T> int qmlRegisterModuleApi(const char *uri, int versionMajor, int versionMinor, QObject *(*callback)(QQmlEngine *, QJSEngine *))
257 This function may be used to register a module API provider \a callback in a particular \a uri
258 with a version specified in \a versionMajor and \a versionMinor.
260 Installing a module API into a uri allows developers to provide arbitrary functionality
261 (methods and properties) in a namespace that doesn't necessarily contain elements.
263 A module API may be either a QObject or a QJSValue. Only one module API provider
264 may be registered into any given namespace (combination of \a uri, \a versionMajor and \a versionMinor).
265 This function should be used to register a module API provider function which returns a QObject
266 of the given type T as a module API.
268 A QObject module API must be imported with a qualifier, and that qualifier may be used as
269 the target in a \l Connections element or otherwise used as any other element id would.
270 One exception to this is that a QObject module API property may not be aliased (because the
271 module API qualifier does not identify an object within the same component as any other item).
273 \b{NOTE:} A QObject module API instance returned from a module API provider is owned by the QML
274 engine. For this reason, the module API provider function should \b{not} be implemented as a
279 // first, define your QObject which provides the functionality.
280 class ModuleApiExample : public QObject
283 Q_PROPERTY (int someProperty READ someProperty WRITE setSomeProperty NOTIFY somePropertyChanged)
286 ModuleApiExample(QObject* parent = 0)
287 : QObject(parent), m_someProperty(0)
291 ~ModuleApiExample() {}
293 Q_INVOKABLE int doSomething() { setSomeProperty(5); return m_someProperty; }
295 int someProperty() const { return m_someProperty; }
296 void setSomeProperty(int val) { m_someProperty = val; emit somePropertyChanged(val); }
299 void somePropertyChanged(int newValue);
305 // second, define the module API provider function (callback).
306 static QObject *example_qobject_module_api_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
309 Q_UNUSED(scriptEngine)
311 ModuleApiExample *example = new ModuleApiExample();
315 // third, register the module API provider with QML by calling this function in an initialization function.
317 qmlRegisterModuleApi<ModuleApiExample>("Qt.example.qobjectApi", 1, 0, example_qobject_module_api_provider);
321 In order to use the registered module API in QML, you must import the module API.
324 import Qt.example.qobjectApi 1.0 as ExampleApi
327 property int someValue: ExampleApi.someProperty
329 Component.onCompleted: {
330 someValue = ExampleApi.doSomething()