1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
8 Declares the enclosing type or namespace to be available in QML, using its
9 class or namespace name as the QML element name.
11 For example, this makes the C++ class \c Slider available as a QML type
15 class Slider : public QObject
23 You can use the build system to register the type in the type namespace
24 \e {com.mycompany.qmlcomponents} with major version \c 1.
25 For qmake, specify the following in your project file:
29 QML_IMPORT_NAME = com.mycompany.qmlcomponents
30 QML_IMPORT_MAJOR_VERSION = 1
33 With CMake, you pass the URI and version to qt_add_qml_module
36 qt6_add_qml_module(myapp
37 URI com.mycompany.qmlcomponents
42 Once registered, the type can be used in QML by importing the
43 same type namespace and version number:
46 import com.mycompany.qmlcomponents 1.0
53 You can also make namespaces tagged with Q_NAMESPACE available this way, in
54 order to expose any enums tagged with Q_ENUM_NS they contain.
56 \b{NOTE:} When classes have the same name but are located in different namespaces using
57 \l QML_ELEMENT on both of them will cause a conflict.
58 Make sure to use \l QML_NAMED_ELEMENT() for one of them instead.
60 \include {qualified-class-name.qdocinc} {class name must be qualified}
62 \sa {Choosing the Correct Integration Method Between C++ and QML}, QML_NAMED_ELEMENT(),
63 Q_REVISION(), QML_ADDED_IN_MINOR_VERSION()
67 \macro QML_NAMED_ELEMENT(name)
70 Declares the enclosing type or namespace to be available in QML, using \a name
71 as the element name. Otherwise behaves the same as QML_ELEMENT.
74 class SqlEventDatabase : public QObject
77 QML_NAMED_ELEMENT(EventDatabase)
83 \sa {Choosing the Correct Integration Method Between C++ and QML}, QML_ELEMENT
90 Declares the enclosing type to be available, but anonymous in QML. The type
91 cannot be created or used to declare properties in QML, but when passed from
92 C++, it is recognized. In QML, you can use properties of this type if they
95 \sa QML_ELEMENT, QML_NAMED_ELEMENT(), QML_UNCREATABLE(), QML_INTERFACE
102 This macro registers the enclosing C++ type in the QML system as an interface.
104 Types registered as an interface in QML should also declare themselves as an
105 interface with the \l {The Meta-Object System}{meta object system}. For
113 virtual ~FooInterface();
114 virtual void doSomething() = 0;
117 Q_DECLARE_INTERFACE(FooInterface, "org.foo.FooInterface")
120 When registered with QML in this way, they can be used as property types:
122 Q_PROPERTY(FooInterface *foo READ foo WRITE setFoo)
124 When you assign a \l QObject sub-class to this property, the QML engine does
125 the interface cast to \c FooInterface* automatically.
127 Interface types are implicitly anonymous and uncreatable in QML.
129 \b{NOTE:} When inheriting from types using QML_INTERFACE, use \l QML_IMPLEMENTS_INTERFACES
130 instead of \l Q_INTERFACES.
132 \sa QML_IMPLEMENTS_INTERFACES(), QML_ELEMENT, QML_NAMED_ELEMENT(), QML_UNCREATABLE(), QML_ANONYMOUS
136 \macro QML_IMPLEMENTS_INTERFACES(interfaces)
139 This macro tells Qt which QML \a interfaces the class implements.
140 This macro should only be used for interfacing with classes using \l QML_INTERFACE, use \l Q_INTERFACES otherwise.
141 It's required in order for declarative registration via \l QML_ELEMENT to
144 \sa QML_INTERFACE, Q_INTERFACES
148 \macro QML_UNCREATABLE(reason)
151 Declares that the enclosing type shall not be creatable from QML. This takes
152 effect if the type is available in QML, by having a \l QML_ELEMENT or
153 \l QML_NAMED_ELEMENT() macro. The \a reason will be emitted as error message if an
154 attempt to create the type from QML is detected.
156 Some QML types are implicitly uncreatable, in particular types exposed with
157 \l QML_ANONYMOUS or namespaces exposed with \l QML_ELEMENT or
158 \l QML_NAMED_ELEMENT().
160 Since Qt 6.0 you can use "" instead of a reason to use a standard message
163 \sa QML_ELEMENT, QML_NAMED_ELEMENT(), QML_ANONYMOUS
170 Declares the enclosing type to be a singleton in QML. This only takes effect
171 if the type is a \l Q_OBJECT and is available in QML (by having a
172 \l QML_ELEMENT or \l QML_NAMED_ELEMENT() macro). By default, each QQmlEngine
173 will try to create a singleton instance using either the type's default
174 constructor or a static factory function of the signature
175 \c{T *create(QQmlEngine *, QJSEngine *)} when the type is first accessed.
176 If both do exist and are accessible, the default constructor is preferred.
177 If there is no default constructor and no factory function the singleton is
178 inaccessible. The QML engine generally assumes ownership of the singleton and
179 will delete it when the engine itself is destroyed. You can prevent this by
180 calling QJSEngine::setObjectOwnership() on the singleton.
182 In order to declare a default-constructible class as singleton, all you have
183 to do is add \l QML_SINGLETON:
186 class MySingleton : public QObject
193 // members, Q_INVOKABLE functions, etc.
197 If the singleton class is not default-constructible, but you can modify it,
198 you can add a factory function to it, in order to make it accessible:
201 class MySingleton : public QObject
209 static MySingleton *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine)
211 MySingleton *result = nullptr;
212 // Create the object using some custom constructor or factory.
213 // The QML engine will assume ownership and delete it, eventually.
217 // members, Q_INVOKABLE functions, etc
221 If you cannot modify the class and it does not have a default constructor or a
222 suitable factory function, you can provide a \l QML_FOREIGN wrapper to define
223 the factory function:
226 struct SingletonForeign
229 QML_FOREIGN(MySingleton)
231 QML_NAMED_ELEMENT(MySingleton)
234 static MySingleton *create(QQmlEngine *, QJSEngine *engine)
236 MySingleton *result = nullptr;
237 // Create the instance using some custom constructor or factory.
238 // The QML engine will assume ownership and delete it, eventually.
244 Finally, if you want to provide one specific singleton object, the creation of
245 which you cannot control, you can return that from a factory function. This is
246 a replacement for the \l qmlRegisterSingletonInstance function.
250 qmlRegisterSingletonInstance("MyModule", 1, 0, "MySingleton", myObject);
253 with myObject being of type \c{MySingleton *}, you can do the following
257 struct SingletonForeign
260 QML_FOREIGN(MySingleton)
262 QML_NAMED_ELEMENT(MySingleton)
265 // Initialize this using myObject where you would previously
266 // call qmlRegisterSingletonInstance().
267 inline static MySingleton *s_singletonInstance = nullptr;
269 static MySingleton *create(QQmlEngine *, QJSEngine *engine)
271 // The instance has to exist before it is used. We cannot replace it.
272 Q_ASSERT(s_singletonInstance);
274 // The engine has to have the same thread affinity as the singleton.
275 Q_ASSERT(engine->thread() == s_singletonInstance->thread());
277 // There can only be one engine accessing the singleton.
279 Q_ASSERT(engine == s_engine);
283 // Explicitly specify C++ ownership so that the engine doesn't delete
285 QJSEngine::setObjectOwnership(s_singletonInstance,
286 QJSEngine::CppOwnership);
287 return s_singletonInstance;
291 inline static QJSEngine *s_engine = nullptr;
295 This way, the pre-existing class \c MySingleton is declared to be a QML
296 singleton, called \c MySingleton. You can specify an instance for it any time
297 before it is used by setting the \c s_singletonInstance member. None of this
298 requires modification of \c MySingleton itself.
300 \note This pattern doesn't work if either the singleton is accessed by
301 multiple QML engines, or if the QML engine accessing it has a different thread
302 affinity than the singleton object itself. As shown above, you can check the
303 parameters to the \c create() method for identity and thread affinity of the
304 engine in order to assert on that.
306 \sa QML_ELEMENT, QML_NAMED_ELEMENT(),
307 qmlRegisterSingletonInstance(), QQmlEngine::singletonInstance()
311 \macro QML_ADDED_IN_MINOR_VERSION(VERSION)
314 Declares that the enclosing type or namespace was added in the specified minor
315 \a VERSION, relative to the module major version. The minor version is assumed
316 to be in line with any revisions given by \l Q_REVISION() macros on methods,
317 slots, or signals, and any REVISION tags on properties declared with
320 \l QML_ADDED_IN_MINOR_VERSION() only takes effect if the type or namespace is
321 available in QML, by having a \l QML_ELEMENT, \l QML_NAMED_ELEMENT(),
322 \l QML_ANONYMOUS, or \l QML_INTERFACE macro.
324 If the QML module the type belongs to is imported with a lower version than
325 the one determined this way, the QML type is invisible.
327 \sa QML_ELEMENT, QML_NAMED_ELEMENT()
331 \macro QML_REMOVED_IN_MINOR_VERSION(VERSION)
334 Declares that the enclosing type or namespace was removed in the specified
335 minor \a VERSION, relative to the module major version. This is primarily
336 useful when replacing the implementation of a QML type. If a corresponding
337 \l QML_ADDED_IN_MINOR_VERSION() is present on a different type or namespace of
338 the same QML name, then the removed type is used when importing versions of
339 the module lower than \a VERSION, and the added type is used when importing
340 versions of the module greater or equal \a VERSION.
342 \l QML_REMOVED_IN_MINOR_VERSION() only takes effect if type or namespace is
343 available in QML, by having a \l QML_ELEMENT, \l QML_NAMED_ELEMENT(),
344 \l QML_ANONYMOUS, or \l QML_INTERFACE macro.
346 \sa QML_ELEMENT, QML_NAMED_ELEMENT()
350 \macro QML_EXTRA_VERSION(MAJOR, MINOR)
353 Declare that the type should also be available in version \a{MAJOR}.\a{MINOR}.
354 This can be helpful if a type should be available in multiple major versions.
356 Types are automatically registered for:
358 \li The major version they were introduced in, see \l{QML_ADDED_IN_VERSION}.
359 \li Any major versions any their members were introduced in.
360 \li The current major version of their module, unless they were
361 \l{QML_REMOVED_IN_VERSION} before that.
364 Notably, they are not automatically registered in any \l{PAST_MAJOR_VERSIONS}
365 between the above. You can use QML_EXTRA_VERSION to manually register your
366 types in further major versions.
368 \note Keeping multiple \l{PAST_MAJOR_VERSIONS} around is computationally
371 \sa QML_ELEMENT, QML_ADDED_IN_MINOR_VERSION
375 \macro QML_ATTACHED(ATTACHED_TYPE)
378 Declares that the enclosing type attaches \a ATTACHED_TYPE as an
379 \l {Attached Properties and Attached Signal Handlers}
380 {attached property} to other types. This takes effect if the type
381 is exposed to QML using a \l QML_ELEMENT or \l QML_NAMED_ELEMENT() macro.
383 \include {qualified-class-name.qdocinc} {class name must be qualified}
385 \sa QML_ELEMENT, QML_NAMED_ELEMENT(), qmlAttachedPropertiesObject(),
386 {Providing Attached Properties}
390 \macro QML_EXTENDED(EXTENDED_TYPE)
393 Declares that the enclosing type uses \a EXTENDED_TYPE as an extension to
394 provide further properties, methods, and enumerations in QML. This takes
395 effect if the type is exposed to QML using a \l QML_ELEMENT or
396 \l QML_NAMED_ELEMENT() macro.
398 \warning Members of \a EXTENDED_TYPE are implicitly treated as FINAL.
400 \include {qualified-class-name.qdocinc} {class name must be qualified}
402 \sa QML_ELEMENT, QML_NAMED_ELEMENT(), QML_EXTENDED_NAMESPACE(),
403 {Registering Extension Objects}
407 \macro QML_EXTENDED_NAMESPACE(EXTENDED_NAMESPACE)
410 Declares that the enclosing type uses \a EXTENDED_NAMESPACE as an extension to
411 provide further enumerations in QML. This takes effect if the type
412 is exposed to QML using a \l QML_ELEMENT or \l QML_NAMED_ELEMENT() macro.
413 The enumerations need to be exposed to the metaobject system for this to work.
415 For example, give the following C++ code
417 namespace MyNamespace {
419 enum MyEnum { MyEnumerator = 10 };
423 class QmlType : public QObject
427 QML_EXTENDED_NAMESPACE(MyNamespace)
431 we can access the enum in QML:
434 property int i: QmlType.MyEnumerator // i will be 10
438 \note EXTENDED_NAMESPACE can also be a QObject or QGadget; in that case - and in contrast to
439 QML_EXTENDED, which also exposes methods and properties - only its enumerations
442 \note \a EXTENDED_NAMESPACE must have a metaobject; i.e. it must either be a namespace which
443 contains the Q_NAMESPACE macro or a QObject/QGadget.
445 \include {qualified-class-name.qdocinc} {class name must be qualified}
447 \sa QML_ELEMENT, QML_NAMED_ELEMENT(), QML_EXTENDED(),
448 {Registering Extension Objects}, Q_ENUM, Q_ENUM_NS
452 \macro QML_FOREIGN(FOREIGN_TYPE)
455 Declares that any \l QML_ELEMENT, \l QML_NAMED_ELEMENT(), \l QML_ANONYMOUS,
456 \l QML_INTERFACE, \l QML_UNCREATABLE(), \l QML_SINGLETON,
457 \l QML_ADDED_IN_MINOR_VERSION(), \l QML_REMOVED_IN_MINOR_VERSION(),
458 \l QML_ATTACHED(), \l QML_EXTENDED(), or \l QML_EXTENDED_NAMESPACE() macros
459 in the enclosing C++ type do not apply to the enclosing type but instead to
460 \a FOREIGN_TYPE. The enclosing type still needs to be registered with the
461 \l {The Meta-Object System}{meta object system} using a \l Q_GADGET or
464 This is useful for registering types that cannot be amended to add the macros,
465 for example because they belong to 3rdparty libraries.
466 To register a namespace, see \l QML_FOREIGN_NAMESPACE().
468 \b{NOTE:} You may want to use \l QML_NAMED_ELEMENT() instead of \l QML_ELEMENT due to the fact that
469 the element will be named like the struct it is contained in, not the foreign type.
470 See the \l {Extension Objects} for an example.
472 \include {qualified-class-name.qdocinc} {class name must be qualified}
474 \sa QML_ELEMENT, QML_NAMED_ELEMENT(), QML_FOREIGN_NAMESPACE()
478 \macro QML_FOREIGN_NAMESPACE(FOREIGN_NAMESPACE)
481 Declares that any \l QML_ELEMENT, \l QML_NAMED_ELEMENT(), \l QML_ANONYMOUS,
482 \l QML_INTERFACE, \l QML_UNCREATABLE(), \l QML_SINGLETON,
483 \l QML_ADDED_IN_MINOR_VERSION(), or \l QML_REMOVED_IN_MINOR_VERSION()
484 macros in the enclosing C++ namespace do not apply to the enclosing type but
485 instead to \a FOREIGN_NAMESPACE. The enclosing namespace still needs to be
486 registered with the \l {The Meta-Object System}{meta object system} using a
487 \l Q_NAMESPACE macro.
489 This is useful for registering namespaces that cannot be amended to add the macros,
490 for example because they belong to 3rdparty libraries.
492 \sa QML_ELEMENT, QML_NAMED_ELEMENT(), QML_FOREIGN()
496 \macro QML_UNAVAILABLE
499 This macro declares the enclosing type to be unavailable in QML. It registers
500 an internal dummy type called \c QQmlTypeNotAvailable as \l QML_FOREIGN()
501 type, using any further QML macros you specify.
503 Normally, the types exported by a module should be fixed. However, if a C++
504 type is not available, you should at least "reserve" the QML type name, and
505 give the user of the unavailable type a meaningful error message.
510 #ifdef NO_GAMES_ALLOWED
514 QML_NAMED_ELEMENT(Game)
516 QML_UNCREATABLE("Get back to work, slacker!");
519 class MinehuntGame : public QObject
522 QML_NAMED_ELEMENT(Game)
528 This will cause any QML which attempts to use the "Game" type to produce an
532 fun.qml: Get back to work, slacker!
537 Using this technique, you only need a \l Q_GADGET struct to customize the error
538 message, not a full-blown \l QObject. Without \l QML_UNCREATABLE(),
539 \l QML_UNAVAILABLE still results in a more specific error message than the usual
540 "is not a type" for completely unknown types.
542 \include {qualified-class-name.qdocinc} {class name must be qualified}
544 \sa QML_ELEMENT, QML_NAMED_ELEMENT(), QML_UNCREATABLE(), QML_FOREIGN()
548 \macro QML_SEQUENTIAL_CONTAINER(VALUE_TYPE)
551 This macro declares the enclosing or referenced type as a sequential container
552 managing a sequence of \a VALUE_TYPE elements. \a VALUE_TYPE can be an actual
553 \l{QML Value Types}{value type} or a pointer to an
554 \l{QML Object Types}{object type}. You will rarely be able to add this macro
555 to the actual container declaration since containers are usually templates.
556 You should use \l{QML_FOREIGN} to attach the type registration to a template
557 instantiation. Using this technique you can, for example, declare sequential
558 containers like this:
561 class IntDequeRegistration
564 QML_FOREIGN(std::deque<int>)
566 QML_SEQUENTIAL_CONTAINER(int)
570 After this, you can use the container like a JavaScript array in QML.
577 // 0: North, 1: East, 2: South, 3: West
578 Q_PROPERTY(std::deque<int> solution READ solution CONSTANT FINAL)
589 function showSolution() {
590 maze.solution.forEach([...])
595 \note For \l{QML Value Types} \l{QList} is automatically registered as
596 sequential container. For \l{QML Object Types} \l{QQmlListProperty} is.
597 You don't have to add these registrations.
599 \note You cannot currently give the container a custom name. Any argument
600 passed to \l{QML_NAMED_ELEMENT} is ignored. The automatically registered
601 sequential containers are available under the familiar \e{list<...>} names,
602 for example \e{list<QtObject>} or \e{list<font>}.
604 \include {qualified-class-name.qdocinc} {class name must be qualified}
606 \sa QML_ANONYMOUS, QML_FOREIGN()
610 \macro QML_DECLARE_TYPE()
613 Equivalent to \c Q_DECLARE_METATYPE(TYPE *) and \c Q_DECLARE_METATYPE(QQmlListProperty<TYPE>)
617 \macro QML_DECLARE_TYPEINFO(Type,Flags)
620 Declares additional properties of the given \a Type as described by the
623 Current the only supported type info is \c QML_HAS_ATTACHED_PROPERTIES which
624 declares that the \a Type supports \l {Attached Properties and Attached Signal Handlers}
625 {attached properties}. QML_DECLARE_TYPEINFO() is not necessary if \a Type contains the
630 \fn void qmlClearTypeRegistrations()
633 Clears all stored type registrations, such as those produced with \l qmlRegisterType().
635 Do not call this function while a QQmlEngine exists or behavior will be undefined.
636 Any existing QQmlEngines must be deleted before calling this function. This function
637 only affects the application global cache. Delete the QQmlEngine to clear all cached
638 data relating to that engine.
643 \fn int qmlRegisterType(const char *uri, int versionMajor, int versionMinor, const char *qmlName)
646 This template function registers the C++ type in the QML system with
647 the name \a qmlName, in the library imported from \a uri having the
648 version number composed from \a versionMajor and \a versionMinor.
650 Returns the QML type id.
652 There are two forms of this template function:
656 int qmlRegisterType(const char *uri, int versionMajor, int versionMinor, const char *qmlName);
658 template<typename T, int metaObjectRevision>
659 int qmlRegisterType(const char *uri, int versionMajor, int versionMinor, const char *qmlName);
662 The former is the standard form which registers the type \e T as a new type.
663 The latter allows a particular revision of a class to be registered in
664 a specified version (see \l {Type Revisions and Versions}).
667 For example, this registers a C++ class \c MySliderItem as a QML type
668 named \c Slider for version 1.0 of a type namespace called
669 "com.mycompany.qmlcomponents":
672 qmlRegisterType<MySliderItem>("com.mycompany.qmlcomponents", 1, 0, "Slider");
675 Once this is registered, the type can be used in QML by importing the
676 specified type namespace and version number:
679 import com.mycompany.qmlcomponents 1.0
686 Note that it's perfectly reasonable for a library to register types to older versions
687 than the actual version of the library. Indeed, it is normal for the new library to allow
688 QML written to previous versions to continue to work, even if more advanced versions of
689 some of its types are available.
691 \sa QML_ELEMENT, QML_NAMED_ELEMENT(),
692 {Choosing the Correct Integration Method Between C++ and QML}
696 \fn int qmlRegisterRevision(const char *uri, int versionMajor, int versionMinor)
699 This template function registers the specified revision of a C++ type in the QML system with
700 the library imported from \a uri having the version number composed
701 from \a versionMajor and \a versionMinor.
703 Returns the QML type id.
706 template<typename T, int metaObjectRevision>
707 int qmlRegisterRevision(const char *uri, int versionMajor, int versionMinor);
710 This function is typically used to register the revision of a base class to
711 use for the specified version of the type (see \l {Type Revisions and Versions}).
715 \fn int qmlRegisterUncreatableType(const char *uri, int versionMajor, int versionMinor, const char *qmlName, const QString& message)
718 This template function registers the C++ type in the QML system with
719 the name \a qmlName, in the library imported from \a uri having the
720 version number composed from \a versionMajor and \a versionMinor.
722 While the type has a name and a type, it cannot be created, and the
723 given error \a message will result if creation is attempted.
725 This is useful where the type is only intended for providing attached properties or enum values.
727 Returns the QML type id.
729 \sa QML_UNCREATABLE(), qmlRegisterTypeNotAvailable(),
730 {Choosing the Correct Integration Method Between C++ and QML}
734 \fn int qmlRegisterExtendedType(const char *uri, int versionMajor, int versionMinor, const char *qmlName)
737 This template function registers the C++ type and its extension object in the
738 QML system with the name \a qmlName in the library imported from \a uri having
739 version number composed from \a versionMajor and \a versionMinor. Properties
740 not available in the main type will be searched for in the extension object.
742 Returns the QML type id.
744 \sa QML_EXTENDED(), qmlRegisterType(), {Registering Extension Objects}
749 \fn int qmlRegisterExtendedUncreatableType(const char *uri, int versionMajor, int versionMinor, const char *qmlName, const QString& reason)
752 This template function registers the C++ type and its extension
753 in the QML system with the name \a qmlName in the library imported
754 from \a uri having version number composed from \a versionMajor and
757 While the type has a name and a type, it cannot be created. An error
758 message with the given \a reason is printed if the user attempts to
759 create an instance of this type.
761 This is useful where the type is only intended for providing attached
762 properties, enum values or an abstract base class with its extension.
764 Returns the QML type id.
766 \sa QML_EXTENDED(), QML_UNCREATABLE(), qmlRegisterUncreatableType()
770 \fn static inline int qmlRegisterUncreatableMetaObject(const QMetaObject &staticMetaObject, const char *uri, int versionMajor, int versionMinor, const char *qmlName, const QString& reason)
774 This function registers the \a staticMetaObject and its extension
775 in the QML system with the name \a qmlName in the library imported
776 from \a uri having version number composed from \a versionMajor and
779 An instance of the meta object cannot be created. An error message with
780 the given \a reason is printed if the user attempts to create it.
782 This function is useful for registering Q_NAMESPACE namespaces.
784 Returns the QML type id.
789 namespace MyNamespace {
799 qmlRegisterUncreatableMetaObject(MyNamespace::staticMetaObject, "io.qt", 1, 0, "MyNamespace", "Access to enums & flags only");
802 On the QML side, you can now use the registered enums:
804 Component.onCompleted: console.log(MyNamespace.Key2)
807 \sa QML_ELEMENT, QML_NAMED_ELEMENT(), QML_UNCREATABLE()
811 \fn int qmlRegisterCustomExtendedType(const char *uri, int versionMajor, int versionMinor, const char *qmlName, QQmlCustomParser *parser)
815 This template function registers the C++ type and its extension
816 in the QML system with the name \a qmlName in the library imported
817 from \a uri having version number composed from \a versionMajor and
818 \a versionMinor. Properties from the C++ type or its extension that
819 cannot be resolved directly by the QML system will be resolved using
820 the \a parser provided.
822 Returns the QML type id.
824 \sa QML_ELEMENT, QML_NAMED_ELEMENT(), QML_EXTENDED()
828 \fn int qmlRegisterTypeNotAvailable(const char *uri, int versionMajor, int versionMinor, const char *qmlName, const QString& message)
831 This function registers a type in the QML system with the name \a qmlName, in the type namespace imported from \a uri having the
832 version number composed from \a versionMajor and \a versionMinor, but any attempt to instantiate the type
833 will produce the given error \a message.
835 Normally, the types exported by a plugin should be fixed. However, if a C++ type is not available, you should
836 at least "reserve" the QML type name, and give the user of the unavailable type a meaningful error message.
838 Returns the QML type id.
843 #ifdef NO_GAMES_ALLOWED
844 qmlRegisterTypeNotAvailable("MinehuntCore", 0, 1, "Game", "Get back to work, slacker!");
846 qmlRegisterType<MinehuntGame>("MinehuntCore", 0, 1, "Game");
850 This will cause any QML which imports the "MinehuntCore" type namespace and attempts to use the type to produce an error message:
852 fun.qml: Get back to work, slacker!
857 Without this, a generic "Game is not a type" message would be given.
859 \sa QML_UNAVAILABLE, qmlRegisterUncreatableType(),
860 {Choosing the Correct Integration Method Between C++ and QML}
864 \fn int qmlRegisterAnonymousType(const char *uri, int versionMajor)
867 This template function registers the C++ type in the QML system as an anonymous type. The
868 resulting QML type does not have a name. Therefore, instances of this type cannot be created from
869 the QML system. You can, however, access instances of the type when they are exposed as properties
872 Use this function when the type will not be referenced by name, specifically for C++ types that
873 are used on the left-hand side of a property binding. To indicate to which module the type belongs
874 use \a uri and \a versionMajor.
876 For example, consider the following two classes:
879 class Bar : public QObject
882 Q_PROPERTY(QString baz READ baz WRITE setBaz NOTIFY bazChanged)
887 QString baz() const { return mBaz; }
889 void setBaz(const QString &baz)
905 class Foo : public QObject
908 Q_PROPERTY(Bar *bar READ bar CONSTANT FINAL)
913 Bar *bar() { return &mBar; }
920 In QML, we assign a string to the \c baz property of \c bar:
925 Component.onCompleted: print(bar.baz)
929 For the QML engine to know that the \c Bar type has a \c baz property,
930 we have to make \c Bar known:
933 qmlRegisterType<Foo>("App", 1, 0, "Foo");
934 qmlRegisterAnonymousType<Bar>("App", 1);
937 As the \c Foo type is instantiated in QML, it must be registered
938 with the version of \l qmlRegisterType() that takes an element name.
940 Returns the QML type id.
943 \sa QML_ANONYMOUS, {Choosing the Correct Integration Method Between C++ and QML}
947 \fn int qmlRegisterInterface(const char *typeName)
950 This template function registers the C++ type in the QML system
951 under the name \a typeName.
953 Types registered as an interface with the engine should also
954 declare themselves as an interface with the
955 \l {The Meta-Object System}{meta object system}. For example:
961 virtual ~FooInterface();
962 virtual void doSomething() = 0;
965 Q_DECLARE_INTERFACE(FooInterface, "org.foo.FooInterface")
968 When registered with the QML engine in this way, they can be used as
971 Q_PROPERTY(FooInterface *foo READ foo WRITE setFoo)
973 When you assign a \l QObject sub-class to this property, the QML engine does
974 the interface cast to \c FooInterface* automatically.
976 Returns the QML type id.
982 \fn int qmlRegisterSingletonType(const char *uri, int versionMajor, int versionMinor, const char *typeName, QJSValue (*callback)(QQmlEngine *, QJSEngine *))
985 This function may be used to register a singleton type provider \a callback in a particular \a uri
986 and \a typeName with a version specified in \a versionMajor and \a versionMinor.
988 Installing a singleton type allows developers to provide arbitrary functionality
989 (methods and properties) to a client without requiring individual instances of the type to
990 be instantiated by the client.
992 A singleton type may be either a QObject or a QJSValue.
993 This function should be used to register a singleton type provider function which returns a QJSValue as a singleton type.
995 \b{NOTE:} QJSValue singleton type properties will \b{not} trigger binding re-evaluation if changed.
999 // First, define the singleton type provider function (callback).
1000 static QJSValue example_qjsvalue_singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
1004 static int seedValue = 5;
1005 QJSValue example = scriptEngine->newObject();
1006 example.setProperty("someProperty", seedValue++);
1010 // Second, register the singleton type provider with QML by calling this function in an initialization function.
1011 qmlRegisterSingletonType("Qt.example.qjsvalueApi", 1, 0, "MyApi", example_qjsvalue_singletontype_provider);
1014 Alternatively, you can use a C++11 lambda:
1017 qmlRegisterSingletonType("Qt.example.qjsvalueApi", 1, 0, "MyApi", [](QQmlEngine *engine, QJSEngine *scriptEngine) -> QJSValue {
1020 static int seedValue = 5;
1021 QJSValue example = scriptEngine->newObject();
1022 example.setProperty("someProperty", seedValue++);
1027 In order to use the registered singleton type in QML, you must import the singleton type.
1030 import Qt.example.qjsvalueApi 1.0 as ExampleApi
1033 property int someValue: ExampleApi.MyApi.someProperty
1037 \sa QML_SINGLETON, {Choosing the Correct Integration Method Between C++ and QML}
1041 \fn template<typename T> QObject *qmlAttachedPropertiesObject(const QObject *attachee, bool create)
1044 The form of this template function is:
1047 template<typename T> QObject *qmlAttachedPropertiesObject(const QObject *attachee, bool create = true)
1050 This returns the attached object instance that has been attached to the specified
1051 \a attachee by the attaching type \e T.
1053 If \a create is true and type \e T is a valid attaching type, this creates and returns a new
1054 attached object instance.
1056 Returns \nullptr if type \e T is not a valid attaching type, or if \a create is false and no
1057 attachment object instance has previously been created for \a attachee.
1059 \sa QML_ATTACHED(), {Providing Attached Properties}
1063 \fn QObject *qmlExtendedObject(const QObject *base)
1066 This function returns the extension object that belongs to \a base, if there is any.
1067 Otherwise it returns \c nullptr.
1073 \fn int qmlRegisterSingletonType(const char *uri, int versionMajor, int versionMinor, const char *typeName, QObject *(*callback)(QQmlEngine *, QJSEngine *))
1076 This function may be used to register a singleton type provider \a callback in a particular \a uri
1077 and \a typeName with a version specified in \a versionMajor and \a versionMinor.
1079 Installing a singleton type into a uri allows developers to provide arbitrary functionality
1080 (methods and properties) to clients without requiring individual instances ot the type to be
1081 instantiated by the client.
1083 A singleton type may be either a QObject or a QJSValue.
1084 This function should be used to register a singleton type provider function which returns a QObject
1085 of the given type T as a singleton type.
1087 A QObject singleton type may be referenced via the type name with which it was registered, and this
1088 typename may be used as the target in a \l Connections type or otherwise used as any other type id would.
1089 One exception to this is that a QObject singleton type property may not be aliased.
1091 \b{NOTE:} A QObject singleton type instance returned from a singleton type provider is owned by
1092 the QML engine unless the object has explicit QQmlEngine::CppOwnership flag set.
1096 // First, define your QObject which provides the functionality.
1097 class SingletonTypeExample : public QObject
1100 Q_PROPERTY (int someProperty READ someProperty WRITE setSomeProperty NOTIFY somePropertyChanged)
1103 SingletonTypeExample(QObject *parent = nullptr)
1104 : QObject(parent), m_someProperty(0)
1108 ~SingletonTypeExample() {}
1110 Q_INVOKABLE int doSomething() { setSomeProperty(5); return m_someProperty; }
1112 int someProperty() const { return m_someProperty; }
1113 void setSomeProperty(int val) { m_someProperty = val; emit somePropertyChanged(val); }
1116 void somePropertyChanged(int newValue);
1122 // Second, define the singleton type provider function (callback).
1123 static QObject *example_qobject_singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
1126 Q_UNUSED(scriptEngine)
1128 SingletonTypeExample *example = new SingletonTypeExample();
1132 // Third, register the singleton type provider with QML by calling this function in an initialization function.
1133 qmlRegisterSingletonType<SingletonTypeExample>("Qt.example.qobjectSingleton", 1, 0, "MyApi", example_qobject_singletontype_provider);
1136 Alternatively, you can use a C++11 lambda:
1139 qmlRegisterSingletonType<SingletonTypeExample>("Qt.example.qobjectSingleton", 1, 0, "MyApi", [](QQmlEngine *engine, QJSEngine *scriptEngine) -> QObject * {
1141 Q_UNUSED(scriptEngine)
1143 SingletonTypeExample *example = new SingletonTypeExample();
1148 In order to use the registered singleton type in QML, you must import the singleton type.
1151 import Qt.example.qobjectSingleton 1.0
1154 property int someValue: MyApi.someProperty
1156 Component.onCompleted: {
1157 someValue = MyApi.doSomething()
1162 \sa QML_SINGLETON, {Choosing the Correct Integration Method Between C++ and QML}
1167 \fn int qmlRegisterSingletonType(const char *uri, int versionMajor, int versionMinor, const char *typeName, std::function<QObject*(QQmlEngine *, QJSEngine *)> callback)
1170 \overload qmlRegisterSingletonType
1175 \fn int qmlRegisterSingletonType(const QUrl &url, const char *uri, int versionMajor, int versionMinor, const char *qmlName)
1178 This function may be used to register a singleton type with the name \a qmlName, in the library imported from \a uri having
1179 the version number composed from \a versionMajor and \a versionMinor. The type is defined by the QML file located at \a url.
1180 The url must be an absolute URL, i.e. url.isRelative() == false.
1182 In addition the type's QML file must have pragma Singleton statement among its import statements.
1184 A singleton type may be referenced via the type name with which it was registered, and this typename may be used as the
1185 target in a \l Connections type or otherwise used as any other type id would. One exception to this is that a singleton
1186 type property may not be aliased (because the singleton type name does not identify an object within the same component
1191 // First, define your QML singleton type which provides the functionality.
1195 property int testProp1: 125
1200 // Second, register the QML singleton type by calling this function in an initialization function.
1201 qmlRegisterSingletonType(QUrl("file:///absolute/path/SingletonType.qml"), "Qt.example.qobjectSingleton", 1, 0, "RegisteredSingleton");
1204 In order to use the registered singleton type in QML, you must import the singleton type.
1207 import Qt.example.qobjectSingleton 1.0
1210 property int someValue: RegisteredSingleton.testProp1
1214 It is also possible to have QML singleton types registered without using the qmlRegisterSingletonType function.
1215 That can be done by adding a pragma Singleton statement among the imports of the type's QML file. In addition
1216 the type must be defined in a qmldir file with a singleton keyword and the qmldir must be imported by the QML
1217 files using the singleton.
1223 \fn int qmlRegisterSingletonInstance(const char *uri, int versionMajor, int versionMinor, const char *typeName, QObject *cppObject)
1227 This function is used to register a singleton object \a cppObject, with a
1228 particular \a uri and \a typeName. Its version is a combination of \a
1229 versionMajor and \a versionMinor.
1231 Installing a singleton type into a URI allows you to provide arbitrary
1232 functionality (methods and properties) to QML code without requiring
1233 individual instances of the type to be instantiated by the client.
1235 Use this function to register an object of the given type T as a singleton
1238 A QObject singleton type may be referenced via the type name with which it
1239 was registered; in turn this type name may be used as the target in a \l
1240 Connections type, or like any other type ID. However, there's one
1241 exception: a QObject singleton type property can't be aliased because the
1242 singleton type name does not identify an object within the same component
1245 \note \a cppObject must outlive the QML engine in which it is used.
1246 Moreover, \cppObject must have the same thread affinity as the engine. If
1247 you want separate singleton instances for multiple engines, you need to use
1248 \l {qmlRegisterSingletonType}. See \l{Threads and QObjects} for more
1249 information about thread safety.
1251 \b{NOTE:} qmlRegisterSingleton can only be used when all types of that module are registered procedurally.
1255 // First, define your QObject which provides the functionality.
1256 class SingletonTypeExample : public QObject
1259 Q_PROPERTY(int someProperty READ someProperty WRITE setSomeProperty NOTIFY somePropertyChanged)
1262 explicit SingletonTypeExample(QObject* parent = nullptr) : QObject(parent) {}
1264 Q_INVOKABLE int doSomething()
1267 return m_someProperty;
1270 int someProperty() const { return m_someProperty; }
1271 void setSomeProperty(int val) {
1272 if (m_someProperty != val) {
1273 m_someProperty = val;
1274 emit somePropertyChanged(val);
1279 void somePropertyChanged(int newValue);
1282 int m_someProperty = 0;
1287 // Second, create an instance of the object
1289 // allocate example before the engine to ensure that it outlives it
1290 QScopedPointer<SingletonTypeExample> example(new SingletonTypeExample);
1293 // Third, register the singleton type provider with QML by calling this
1294 // function in an initialization function.
1295 qmlRegisterSingletonInstance("Qt.example.qobjectSingleton", 1, 0, "MyApi", example.get());
1299 In order to use the registered singleton type in QML, you must import the
1300 URI with the corresponding version.
1303 import Qt.example.qobjectSingleton 1.0
1306 property int someValue: MyApi.someProperty
1308 Component.onCompleted: {
1309 console.log(MyApi.doSomething())
1314 \sa QML_SINGLETON, qmlRegisterSingletonType
1318 \fn int qmlRegisterType(const QUrl &url, const char *uri, int versionMajor, int versionMinor, const char *qmlName);
1321 This function registers a type in the QML system with the name \a qmlName, in the library imported from \a uri having the
1322 version number composed from \a versionMajor and \a versionMinor. The type is defined by the QML file located at \a url. The
1323 url must be an absolute URL, i.e. url.isRelative() == false.
1325 Normally QML files can be loaded as types directly from other QML files, or using a qmldir file. This function allows
1326 registration of files to types from C++ code, such as when the type mapping needs to be procedurally determined at startup.
1328 Returns -1 if the registration was not successful.
1332 \fn bool qmlProtectModule(const char* uri, int majVersion);
1335 This function protects a module from further modification. This can be used
1336 to prevent other plugins from injecting types into your module. It can also
1337 be a performance improvement, as it allows the engine to skip checking for
1338 the possibility of new types or plugins when this import is reached.
1340 Once qmlProtectModule has been called, a QML engine will not search for a new
1341 \c qmldir file to load the module anymore. It will re-use any \c qmldir files
1342 it has loaded before, though. Therefore, types present at this point continue
1343 to work. Mind that different QML engines may load different modules. The
1344 module protection, however, is global and affects all engines. The overhead
1345 of locating \c qmldir files and loading plugins may be noticeable with slow file
1346 systems. Therefore, protecting a module once you are sure you won't need to
1347 load it anymore can be a good optimization. Mind also that the module lock
1348 not only affects plugins but also any other qmldir directives, like \c import
1349 or \c prefer, as well as any composite types or scripts declared in a \c qmldir
1352 In addition, after this function is called, any attempt to register C++ types
1353 into this uri, major version combination will lead to a runtime error.
1355 Returns true if the module with \a uri as a \l{Identified Modules}
1356 {module identifier} and \a majVersion as a major version number was found
1357 and locked, otherwise returns false. The module must contain exported types
1358 in order to be found.
1363 \fn void qmlRegisterModule(const char* uri, int versionMajor, int versionMinor);
1366 This function registers a module in a particular \a uri with a version specified
1367 in \a versionMajor and \a versionMinor.
1369 This can be used to make a certain module version available, even if no types
1370 are registered for that version. This is particularly useful for keeping the
1371 versions of related modules in sync.
1376 \fn int qmlTypeId(const char* uri, int versionMajor, int versionMinor, const char *qmlName);
1379 Returns the QML type id of a type that was registered with the
1380 name \a qmlName in a particular \a uri and a version specified in \a
1381 versionMajor and \a versionMinor.
1383 This function returns the same value as the QML type registration functions
1384 such as qmlRegisterType() and qmlRegisterSingletonType().
1386 If \a qmlName, \a uri and \a versionMajor match a registered type, but the
1387 specified minor version in \a versionMinor is higher, then the id of the type
1388 with the closest minor version is returned.
1390 Returns -1 if no matching type was found or one of the given parameters
1393 \sa QML_ELEMENT, QML_NAMED_ELEMENT, QML_SINGLETON, qmlRegisterType(), qmlRegisterSingletonType()
1397 \macro QML_VALUE_TYPE(name)
1400 Declares the enclosing type or namespace to be available in QML, using \a name
1401 as the name. The type has to be a value type and the name has to be lower case.
1407 QML_VALUE_TYPE(myValueType)
1413 \sa {Choosing the Correct Integration Method Between C++ and QML}, QML_NAMED_ELEMENT
1417 \macro QML_CONSTRUCTIBLE_VALUE
1421 Marks the surrounding value type as constructible. That is, any \l Q_INVOKABLE
1422 constructors of the type that take exactly one argument can be used when
1423 assigning a JavaScript value to a property of this type.
1425 You can declare a constructible value type as follows:
1431 QML_VALUE_TYPE(myValueType)
1432 QML_CONSTRUCTIBLE_VALUE
1433 Q_INVOKABLE MyValueType(double d);
1439 With the above type, the following QML code will produce a \c MyValueType
1440 value using the given constructor and assign it to the property.
1444 property myValueType v: 5.4
1452 \macro QML_STRUCTURED_VALUE
1456 Marks the surrounding value type as structured. Structured value types can
1457 and will preferably be constructed property-by-property from a JavaScript
1458 object. A structured value type, however is always \l QML_CONSTRUCTIBLE_VALUE,
1459 too. This means, you can still provide \l Q_INVOKABLE constructors in order to
1460 handle construction from primitive types.
1462 You can declare a structured value type as follows:
1468 QML_VALUE_TYPE(myValueType)
1469 QML_STRUCTURED_VALUE
1470 Q_PROPERTY(double d READ d WRITE setD)
1471 Q_PROPERTY(string e READ e WRITE setE)
1477 Then you can populate a property of this type as follows:
1481 property myValueType v: ({d: 4.4, e: "a string"})
1485 The extra parentheses are necessary to disambiguate the JavaScript object
1486 from what might be interpreted as a JavaScript code block.
1488 \sa QML_VALUE_TYPE QML_CONSTRUCTIBLE_VALUE