Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qmlfunctions.qdoc
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5 \macro QML_ELEMENT
6 \relates QQmlEngine
7
8 Declares the enclosing type or namespace to be available in QML, using its
9 class or namespace name as the QML element name.
10
11 For example, this makes the C++ class \c Slider available as a QML type
12 named \c Slider. All its properties, invokable methods and enums are exposed.
13
14 \code
15 class Slider : public QObject
16 {
17 Q_OBJECT
18 QML_ELEMENT
19 Q_PROPERTY(int value READ value WRITE setValue NOTIFY valueChanged FINAL)
20 // ...
21 public:
22 enum Slippiness {
23 Dry, Wet, Icy
24 };
25 Q_ENUM(Slippiness)
26
27 Q_INVOKABLE void slide(Slippiness slippiness);
28
29 // ...
30 }
31 \endcode
32
33 You can use the build system to register the type in the type namespace
34 \e {com.mycompany.qmlcomponents} with major version \c 1.
35 For qmake, specify the following in your project file:
36
37 \badcode
38 CONFIG += qmltypes
39 QML_IMPORT_NAME = com.mycompany.qmlcomponents
40 QML_IMPORT_MAJOR_VERSION = 1
41 \endcode
42
43 With CMake, you pass the URI and version to qt_add_qml_module
44
45 \badcode
46 qt6_add_qml_module(myapp
47 URI com.mycompany.qmlcomponents
48 VERSION 1.0
49 )
50 \endcode
51
52 Once registered, the type can be used in QML by importing the
53 same type namespace and version number:
54
55 \qml
56 import com.mycompany.qmlcomponents 1.0
57
58 Slider {
59 value: 12
60 Component.onCompleted: slide(Slider.Icy)
61
62 // ...
63 }
64 \endqml
65
66 You can also make namespaces tagged with Q_NAMESPACE available this way, in
67 order to expose any enums tagged with Q_ENUM_NS they contain:
68
69 \code
70 namespace MyNamespace {
71 Q_NAMESPACE
72 QML_ELEMENT
73
74 enum MyEnum {
75 Key1,
76 Key2,
77 };
78 Q_ENUM_NS(MyEnum)
79 }
80 \endcode
81
82 In QML, you can then use the enums:
83
84 \qml
85 Component.onCompleted: console.log(MyNamespace.Key2)
86 \endqml
87
88 \b{NOTE:} When classes have the same name but are located in different namespaces using
89 \l QML_ELEMENT on both of them will cause a conflict.
90 Make sure to use \l QML_NAMED_ELEMENT() for one of them instead.
91
92 \include {qualified-class-name.qdocinc} {class name must be qualified}
93
94 \sa {Choosing the Correct Integration Method Between C++ and QML}, QML_NAMED_ELEMENT(),
95 Q_REVISION(), QML_ADDED_IN_VERSION()
96*/
97
98/*!
99 \macro QML_NAMED_ELEMENT(name)
100 \relates QQmlEngine
101
102 Declares the enclosing type or namespace to be available in QML, using \a name
103 as the element name. Otherwise behaves the same as QML_ELEMENT.
104
105 \code
106 class SqlEventDatabase : public QObject
107 {
108 Q_OBJECT
109 QML_NAMED_ELEMENT(EventDatabase)
110
111 // ...
112 };
113 \endcode
114
115 \sa {Choosing the Correct Integration Method Between C++ and QML}, QML_ELEMENT
116*/
117
118/*!
119 \macro QML_ANONYMOUS
120 \relates QQmlEngine
121
122 Declares the enclosing type to be available, but anonymous in QML. The type
123 cannot be created or used to declare properties in QML, but when passed from
124 C++, it is recognized. In QML, you can use properties of this type if they
125 are declared in C++.
126
127 \sa QML_ELEMENT, QML_NAMED_ELEMENT(), QML_UNCREATABLE(), QML_INTERFACE
128*/
129
130/*!
131 \macro QML_INTERFACE
132 \relates QQmlEngine
133
134 This macro registers the enclosing C++ type in the QML system as an interface.
135
136 Types registered as an interface in QML should also declare themselves as an
137 interface with the \l {The Meta-Object System}{meta object system}. For
138 example:
139
140 \code
141 struct FooInterface
142 {
143 QML_INTERFACE
144 public:
145 virtual ~FooInterface();
146 virtual void doSomething() = 0;
147 };
148
149 Q_DECLARE_INTERFACE(FooInterface, "org.foo.FooInterface")
150 \endcode
151
152 When registered with QML in this way, they can be used as property types:
153
154 Q_PROPERTY(FooInterface *foo READ foo WRITE setFoo)
155
156 When you assign a \l QObject sub-class to this property, the QML engine does
157 the interface cast to \c FooInterface* automatically.
158
159 Interface types are implicitly anonymous and uncreatable in QML.
160
161 \b{NOTE:} When inheriting from types using QML_INTERFACE, use \l QML_IMPLEMENTS_INTERFACES
162 instead of \l Q_INTERFACES.
163
164 \sa QML_IMPLEMENTS_INTERFACES(), QML_ELEMENT, QML_NAMED_ELEMENT(), QML_UNCREATABLE(), QML_ANONYMOUS
165*/
166
167/*!
168 \macro QML_IMPLEMENTS_INTERFACES(interfaces)
169 \relates QQmlEngine
170
171 This macro tells Qt which QML \a interfaces the class implements.
172 This macro should only be used for interfacing with classes using \l QML_INTERFACE, use \l Q_INTERFACES otherwise.
173 It's required in order for declarative registration via \l QML_ELEMENT to
174 function properly.
175
176 \sa QML_INTERFACE, Q_INTERFACES
177*/
178
179/*!
180 \macro QML_UNCREATABLE(reason)
181 \relates QQmlEngine
182
183 Declares that the enclosing type shall not be creatable from QML. This takes
184 effect if the type is available in QML, by having a \l QML_ELEMENT or
185 \l QML_NAMED_ELEMENT() macro. The \a reason will be emitted as error message if an
186 attempt to create the type from QML is detected.
187
188 Some QML types are implicitly uncreatable, in particular types exposed with
189 \l QML_ANONYMOUS or namespaces exposed with \l QML_ELEMENT or
190 \l QML_NAMED_ELEMENT().
191
192 Since Qt 6.0 you can use "" instead of a reason to use a standard message
193 instead.
194
195 \sa QML_ELEMENT, QML_NAMED_ELEMENT(), QML_ANONYMOUS
196*/
197
198/*!
199 \macro QML_SINGLETON
200 \relates QQmlEngine
201
202 Declares the enclosing type to be a singleton in QML. This only takes effect
203 if the type is a \l Q_OBJECT and is available in QML (by having a
204 \l QML_ELEMENT or \l QML_NAMED_ELEMENT() macro). By default, each QQmlEngine
205 will try to create a singleton instance using either the type's default
206 constructor or a static factory function of the signature
207 \c{T *create(QQmlEngine *, QJSEngine *)} when the type is first accessed.
208 If both do exist and are accessible, the default constructor is preferred.
209 If there is no default constructor and no factory function the singleton is
210 inaccessible. The QML engine generally assumes ownership of the singleton and
211 will delete it when the engine itself is destroyed. You can prevent this by
212 calling QJSEngine::setObjectOwnership() on the singleton.
213
214 In order to declare a default-constructible class as singleton, all you have
215 to do is add \l QML_SINGLETON:
216
217 \code
218 class MySingleton : public QObject
219 {
220 Q_OBJECT
221 QML_ELEMENT
222 QML_SINGLETON
223 // Q_PROPERTY( ... )
224 public:
225 // members, Q_INVOKABLE functions, etc.
226 };
227 \endcode
228
229 If the singleton class is not default-constructible, but you can modify it,
230 you can add a factory function to it, in order to make it accessible:
231
232 \code
233 class MySingleton : public QObject
234 {
235 Q_OBJECT
236 QML_ELEMENT
237 QML_SINGLETON
238 // Q_PROPERTY( ... )
239
240 public:
241 static MySingleton *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine)
242 {
243 MySingleton *result = nullptr;
244 // Create the object using some custom constructor or factory.
245 // The QML engine will assume ownership and delete it, eventually.
246 return result;
247 }
248
249 // members, Q_INVOKABLE functions, etc
250 };
251 \endcode
252
253 If you cannot modify the class and it does not have a default constructor or a
254 suitable factory function, you can provide a \l QML_FOREIGN wrapper to define
255 the factory function:
256
257 \code
258 struct SingletonForeign
259 {
260 Q_GADGET
261 QML_FOREIGN(MySingleton)
262 QML_SINGLETON
263 QML_NAMED_ELEMENT(MySingleton)
264 public:
265
266 static MySingleton *create(QQmlEngine *, QJSEngine *engine)
267 {
268 MySingleton *result = nullptr;
269 // Create the instance using some custom constructor or factory.
270 // The QML engine will assume ownership and delete it, eventually.
271 return result;
272 }
273 };
274 \endcode
275
276 Finally, if you want to provide one specific singleton object, the creation of
277 which you cannot control, you can return that from a factory function. This is
278 a replacement for the \l qmlRegisterSingletonInstance function.
279 If you were calling
280
281 \code
282 qmlRegisterSingletonInstance("MyModule", 1, 0, "MySingleton", myObject);
283 \endcode
284
285 with myObject being of type \c{MySingleton *}, you can do the following
286 instead:
287
288 \code
289 struct SingletonForeign
290 {
291 Q_GADGET
292 QML_FOREIGN(MySingleton)
293 QML_SINGLETON
294 QML_NAMED_ELEMENT(MySingleton)
295 public:
296
297 // Initialize this using myObject where you would previously
298 // call qmlRegisterSingletonInstance().
299 inline static MySingleton *s_singletonInstance = nullptr;
300
301 static MySingleton *create(QQmlEngine *, QJSEngine *engine)
302 {
303 // The instance has to exist before it is used. We cannot replace it.
304 Q_ASSERT(s_singletonInstance);
305
306 // The engine has to have the same thread affinity as the singleton.
307 Q_ASSERT(engine->thread() == s_singletonInstance->thread());
308
309 // There can only be one engine accessing the singleton.
310 if (s_engine)
311 Q_ASSERT(engine == s_engine);
312 else
313 s_engine = engine;
314
315 // Explicitly specify C++ ownership so that the engine doesn't delete
316 // the instance.
317 QJSEngine::setObjectOwnership(s_singletonInstance,
318 QJSEngine::CppOwnership);
319 return s_singletonInstance;
320 }
321
322 private:
323 inline static QJSEngine *s_engine = nullptr;
324 };
325 \endcode
326
327 This way, the pre-existing class \c MySingleton is declared to be a QML
328 singleton, called \c MySingleton. You can specify an instance for it any time
329 before it is used by setting the \c s_singletonInstance member. None of this
330 requires modification of \c MySingleton itself.
331
332 \note This pattern doesn't work if either the singleton is accessed by
333 multiple QML engines, or if the QML engine accessing it has a different thread
334 affinity than the singleton object itself. As shown above, you can check the
335 parameters to the \c create() method for identity and thread affinity of the
336 engine in order to assert on that.
337
338 \sa QML_ELEMENT, QML_NAMED_ELEMENT(),
339 qmlRegisterSingletonInstance(), QQmlEngine::singletonInstance(), {Singletons in QML}
340*/
341
342/*!
343 \macro QML_ADDED_IN_VERSION(MAJOR, MINOR)
344 \relates QQmlEngine
345
346 Declares that the enclosing type or namespace was added in the specified
347 \a{MAJOR}.\a{MINOR} version. The version is assumed to be in line with any
348 revisions given by \l Q_REVISION() macros on methods, slots, or signals,
349 and any REVISION() attributes on properties declared with \l Q_PROPERTY().
350
351 \l QML_ADDED_IN_VERSION() only takes effect if the type or namespace is
352 available in QML, by having a \l QML_ELEMENT, \l QML_NAMED_ELEMENT(),
353 \l QML_ANONYMOUS, or \l QML_INTERFACE macro.
354
355 If the QML module the type belongs to is imported with a lower version than
356 the one determined this way, the QML type is invisible.
357
358 \sa QML_ELEMENT, QML_NAMED_ELEMENT
359*/
360
361/*!
362 \macro QML_ADDED_IN_MINOR_VERSION(VERSION)
363 \relates QQmlEngine
364 \deprecated [6.7] Use QML_ADDED_IN_VERSION and specify the full version
365
366 Declares that the enclosing type or namespace was added in the specified minor
367 \a VERSION, relative to the module major version. The minor version is assumed
368 to be in line with any revisions given by \l Q_REVISION() macros on methods,
369 slots, or signals, and any REVISION() attributes on properties declared with
370 \l Q_PROPERTY().
371
372 \l QML_ADDED_IN_MINOR_VERSION() only takes effect if the type or namespace is
373 available in QML, by having a \l QML_ELEMENT, \l QML_NAMED_ELEMENT(),
374 \l QML_ANONYMOUS, or \l QML_INTERFACE macro.
375
376 If the QML module the type belongs to is imported with a lower version than
377 the one determined this way, the QML type is invisible.
378
379 \sa QML_ADDED_IN_VERSION, QML_ELEMENT, QML_NAMED_ELEMENT
380*/
381
382/*!
383 \macro QML_REMOVED_IN_VERSION(MAJOR, MINOR)
384 \relates QQmlEngine
385
386 Declares that the enclosing type or namespace was removed in the specified
387 \a{MAJOR}.\a{MINOR} version. This is primarily useful when replacing the
388 implementation of a QML type. If a corresponding \l QML_ADDED_IN_VERSION()
389 is present on a different type or namespace of the same QML name, then the
390 removed type is used when importing versions of the module lower than
391 \a{MAJOR}.\a{MINOR}, and the added type is used when importing
392 versions of the module greater or equal \a{MAJOR}.\a{MINOR}.
393
394 \l QML_REMOVED_IN_VERSION() only takes effect if type or namespace is
395 available in QML, by having a \l QML_ELEMENT, \l QML_NAMED_ELEMENT(),
396 \l QML_ANONYMOUS, or \l QML_INTERFACE macro.
397
398 \sa QML_ELEMENT, QML_NAMED_ELEMENT
399*/
400
401/*!
402 \macro QML_REMOVED_IN_MINOR_VERSION(VERSION)
403 \relates QQmlEngine
404 \deprecated [6.7] Use QML_REMOVED_IN_VERSION and specify the full version
405
406 Declares that the enclosing type or namespace was removed in the specified
407 minor \a VERSION, relative to the module major version. This is primarily
408 useful when replacing the implementation of a QML type. If a corresponding
409 \l QML_ADDED_IN_VERSION() is present on a different type or namespace of
410 the same QML name, then the removed type is used when importing versions of
411 the module lower than \a VERSION, and the added type is used when importing
412 versions of the module greater or equal \a VERSION.
413
414 \l QML_REMOVED_IN_MINOR_VERSION() only takes effect if type or namespace is
415 available in QML, by having a \l QML_ELEMENT, \l QML_NAMED_ELEMENT(),
416 \l QML_ANONYMOUS, or \l QML_INTERFACE macro.
417
418 \sa QML_REMOVED_IN_VERSION, QML_ELEMENT, QML_NAMED_ELEMENT
419*/
420
421/*!
422 \macro QML_EXTRA_VERSION(MAJOR, MINOR)
423 \relates QQmlEngine
424
425 Declare that the type should also be available in version \a{MAJOR}.\a{MINOR}.
426 This can be helpful if a type should be available in multiple major versions.
427
428 Types are automatically registered for:
429 \list
430 \li The major version they were introduced in, see \l{QML_ADDED_IN_VERSION}.
431 \li Any major versions any their members were introduced in.
432 \li The current major version of their module, unless they were
433 \l{QML_REMOVED_IN_VERSION} before that.
434 \endlist
435
436 Notably, they are not automatically registered in any \l{PAST_MAJOR_VERSIONS}
437 between the above. You can use QML_EXTRA_VERSION to manually register your
438 types in further major versions.
439
440 \note Keeping multiple \l{PAST_MAJOR_VERSIONS} around is computationally
441 expensive.
442
443 \sa QML_ELEMENT, QML_ADDED_IN_VERSION
444*/
445
446/*!
447 \macro QML_ATTACHED(ATTACHED_TYPE)
448 \relates QQmlEngine
449
450 Declares that the enclosing type attaches \a ATTACHED_TYPE as an
451 \l {Attached Properties and Attached Signal Handlers}
452 {attached property} to other types. This takes effect if the type
453 is exposed to QML using a \l QML_ELEMENT or \l QML_NAMED_ELEMENT() macro.
454
455 \include {qualified-class-name.qdocinc} {class name must be qualified}
456
457 \sa QML_ELEMENT, QML_NAMED_ELEMENT(), qmlAttachedPropertiesObject(),
458 {Providing Attached Properties}
459*/
460
461/*!
462 \macro QML_EXTENDED(EXTENDED_TYPE)
463 \relates QQmlEngine
464
465 Declares that the enclosing type uses \a EXTENDED_TYPE as an extension to
466 provide further properties, methods, and enumerations in QML. This takes
467 effect if the type is exposed to QML using a \l QML_ELEMENT or
468 \l QML_NAMED_ELEMENT() macro.
469
470 \warning Members of \a EXTENDED_TYPE are implicitly treated as FINAL.
471
472 \include {qualified-class-name.qdocinc} {class name must be qualified}
473
474 \sa QML_ELEMENT, QML_NAMED_ELEMENT(), QML_EXTENDED_NAMESPACE(),
475 {Registering Extension Objects}
476*/
477
478/*!
479 \macro QML_EXTENDED_NAMESPACE(EXTENDED_NAMESPACE)
480 \relates QQmlEngine
481
482 Declares that the enclosing type uses \a EXTENDED_NAMESPACE as an extension to
483 provide further enumerations in QML. This takes effect if the type
484 is exposed to QML using a \l QML_ELEMENT or \l QML_NAMED_ELEMENT() macro.
485 The enumerations need to be exposed to the metaobject system for this to work.
486
487 For example, give the following C++ code
488 \code
489 namespace MyNamespace {
490 Q_NAMESPACE
491 enum MyEnum { MyEnumerator = 10 };
492 Q_ENUM_NS(MyEnum)
493 }
494
495 class QmlType : public QObject
496 {
497 Q_OBJECT
498 QML_ELEMENT
499 QML_EXTENDED_NAMESPACE(MyNamespace)
500 }
501 \endcode
502
503 we can access the enum in QML:
504 \qml
505 QmlType {
506 property int i: QmlType.MyEnumerator // i will be 10
507 }
508 \endqml
509
510 \note EXTENDED_NAMESPACE can also be a QObject or QGadget; in that case - and in contrast to
511 QML_EXTENDED, which also exposes methods and properties - only its enumerations
512 are exposed.
513
514 \note \a EXTENDED_NAMESPACE must have a metaobject; i.e. it must either be a namespace which
515 contains the Q_NAMESPACE macro or a QObject/QGadget.
516
517 \include {qualified-class-name.qdocinc} {class name must be qualified}
518
519 \sa QML_ELEMENT, QML_NAMED_ELEMENT(), QML_EXTENDED(),
520 {Registering Extension Objects}, Q_ENUM, Q_ENUM_NS
521*/
522
523/*!
524 \macro QML_FOREIGN(FOREIGN_TYPE)
525 \relates QQmlEngine
526
527 Declares that any \l QML_ELEMENT, \l QML_NAMED_ELEMENT(), \l QML_ANONYMOUS,
528 \l QML_INTERFACE, \l QML_UNCREATABLE(), \l QML_SINGLETON,
529 \l QML_ADDED_IN_VERSION(), \l QML_REMOVED_IN_VERSION(),
530 \l QML_ADDED_IN_MINOR_VERSION(), \l QML_REMOVED_IN_MINOR_VERSION(),
531 \l QML_EXTENDED(), or \l QML_EXTENDED_NAMESPACE() macros
532 in the enclosing C++ type do not apply to the enclosing type but instead to
533 \a FOREIGN_TYPE. The enclosing type still needs to be registered with the
534 \l {The Meta-Object System}{meta object system} using a \l Q_GADGET or
535 \l Q_OBJECT macro.
536
537 This is useful for registering types that cannot be amended to add the macros,
538 for example because they belong to 3rdparty libraries.
539 To register a namespace, see \l QML_FOREIGN_NAMESPACE().
540
541 \note You may want to use \l QML_NAMED_ELEMENT() instead of \l QML_ELEMENT.
542 With QML_ELEMENT, the element is named after the struct it is contained in,
543 not the foreign type. The \l {Foreign objects integration} chapter in
544 \l {Writing advanced QML Extensions with C++} demonstrates this.
545
546 \note QML_ATTACHED() can currently not be redirected like this. It has to be
547 specificed in the same type that implements qmlAttachedProperties().
548
549 \include {qualified-class-name.qdocinc} {class name must be qualified}
550
551 \sa QML_ELEMENT, QML_NAMED_ELEMENT(), QML_FOREIGN_NAMESPACE()
552*/
553
554/*!
555 \macro QML_FOREIGN_NAMESPACE(FOREIGN_NAMESPACE)
556 \relates QQmlEngine
557
558 Declares that any \l QML_ELEMENT, \l QML_NAMED_ELEMENT(), \l QML_ANONYMOUS,
559 \l QML_INTERFACE, \l QML_UNCREATABLE(), \l QML_SINGLETON,
560 \l QML_ADDED_IN_VERSION(), \l QML_REMOVED_IN_VERSION(),
561 \l QML_ADDED_IN_MINOR_VERSION(), or \l QML_REMOVED_IN_MINOR_VERSION()
562 macros in the enclosing C++ namespace do not apply to the enclosing type but
563 instead to \a FOREIGN_NAMESPACE. The enclosing namespace still needs to be
564 registered with the \l {The Meta-Object System}{meta object system} using a
565 \l Q_NAMESPACE macro.
566
567 This is useful for registering namespaces that cannot be amended to add the macros,
568 for example because they belong to 3rdparty libraries.
569
570 \sa QML_ELEMENT, QML_NAMED_ELEMENT(), QML_FOREIGN()
571*/
572
573/*!
574 \macro QML_UNAVAILABLE
575 \relates QQmlEngine
576
577 This macro declares the enclosing type to be unavailable in QML. It registers
578 an internal dummy type called \c QQmlTypeNotAvailable as \l QML_FOREIGN()
579 type, using any further QML macros you specify.
580
581 Normally, the types exported by a module should be fixed. However, if a C++
582 type is not available, you should at least "reserve" the QML type name, and
583 give the user of the unavailable type a meaningful error message.
584
585 Example:
586
587 \code
588 #ifdef NO_GAMES_ALLOWED
589 struct MinehuntGame
590 {
591 Q_GADGET
592 QML_NAMED_ELEMENT(Game)
593 QML_UNAVAILABLE
594 QML_UNCREATABLE("Get back to work, slacker!");
595 };
596 #else
597 class MinehuntGame : public QObject
598 {
599 Q_OBJECT
600 QML_NAMED_ELEMENT(Game)
601 // ...
602 };
603 #endif
604 \endcode
605
606 This will cause any QML which attempts to use the "Game" type to produce an
607 error message:
608
609 \badcode
610 fun.qml: Get back to work, slacker!
611 Game {
612 ^
613 \endcode
614
615 Using this technique, you only need a \l Q_GADGET struct to customize the error
616 message, not a full-blown \l QObject. Without \l QML_UNCREATABLE(),
617 \l QML_UNAVAILABLE still results in a more specific error message than the usual
618 "is not a type" for completely unknown types.
619
620 \include {qualified-class-name.qdocinc} {class name must be qualified}
621
622 \sa QML_ELEMENT, QML_NAMED_ELEMENT(), QML_UNCREATABLE(), QML_FOREIGN()
623*/
624
625/*!
626 \macro QML_SEQUENTIAL_CONTAINER(VALUE_TYPE)
627 \relates QQmlEngine
628
629 This macro declares the enclosing or referenced type as a sequential container
630 managing a sequence of \a VALUE_TYPE elements. \a VALUE_TYPE can be an actual
631 \l{QML Value Types}{value type} or a pointer to an
632 \l{QML Object Types}{object type}. You will rarely be able to add this macro
633 to the actual container declaration since containers are usually templates.
634 You should use \l{QML_FOREIGN} to attach the type registration to a template
635 instantiation. Using this technique you can, for example, declare sequential
636 containers like this:
637
638 \code
639 class IntDequeRegistration
640 {
641 Q_GADGET
642 QML_FOREIGN(std::deque<int>)
643 QML_ANONYMOUS
644 QML_SEQUENTIAL_CONTAINER(int)
645 };
646 \endcode
647
648 After this, you can use the container like a JavaScript array in QML.
649
650 \code
651 class Maze
652 {
653 Q_OBJECT
654 Q_ELEMENT
655 // 0: North, 1: East, 2: South, 3: West
656 Q_PROPERTY(std::deque<int> solution READ solution CONSTANT FINAL)
657 [...]
658 }
659 \endcode
660
661 \code
662 Item {
663 Maze {
664 id: maze
665 }
666
667 function showSolution() {
668 maze.solution.forEach([...])
669 }
670 }
671 \endcode
672
673 \note For \l{QML Value Types} \l{QList} is automatically registered as
674 sequential container. For \l{QML Object Types} \l{QQmlListProperty} is.
675 You don't have to add these registrations.
676
677 \note You cannot currently give the container a custom name. Any argument
678 passed to \l{QML_NAMED_ELEMENT} is ignored. The automatically registered
679 sequential containers are available under the familiar \e{list<...>} names,
680 for example \e{list<QtObject>} or \e{list<font>}.
681
682 \include {qualified-class-name.qdocinc} {class name must be qualified}
683
684 \sa QML_ANONYMOUS, QML_FOREIGN()
685*/
686
687/*!
688 \macro QML_DECLARE_TYPE()
689 \relates QQmlEngine
690
691 Equivalent to \c Q_DECLARE_METATYPE(TYPE *) and \c Q_DECLARE_METATYPE(QQmlListProperty<TYPE>)
692*/
693
694/*!
695 \macro QML_DECLARE_TYPEINFO(Type,Flags)
696 \relates QQmlEngine
697
698 Declares additional properties of the given \a Type as described by the
699 specified \a Flags.
700
701 Current the only supported type info is \c QML_HAS_ATTACHED_PROPERTIES which
702 declares that the \a Type supports \l {Attached Properties and Attached Signal Handlers}
703 {attached properties}. QML_DECLARE_TYPEINFO() is not necessary if \a Type contains the
704 QML_ATTACHED macro.
705*/
706
707/*!
708 \fn void qmlClearTypeRegistrations()
709 \relates QQmlEngine
710
711 Clears all stored type registrations, such as those produced with \l qmlRegisterType().
712
713 Do not call this function while a QQmlEngine exists or behavior will be undefined.
714 Any existing QQmlEngines must be deleted before calling this function. This function
715 only affects the application global cache. Delete the QQmlEngine to clear all cached
716 data relating to that engine.
717*/
718
719
720/*!
721 \fn template <typename T> int qmlRegisterType(const char *uri, int versionMajor, int versionMinor, const char *qmlName)
722 \relates QQmlEngine
723
724 This template function registers the C++ type in the QML system with
725 the name \a qmlName, in the library imported from \a uri having the
726 version number composed from \a versionMajor and \a versionMinor.
727
728 Returns the QML type id.
729
730 There are two forms of this template function:
731
732 \code
733 template<typename T>
734 int qmlRegisterType(const char *uri, int versionMajor, int versionMinor, const char *qmlName);
735
736 template<typename T, int metaObjectRevision>
737 int qmlRegisterType(const char *uri, int versionMajor, int versionMinor, const char *qmlName);
738 \endcode
739
740 The former is the standard form which registers the type \e T as a new type.
741 The latter allows a particular revision of a class to be registered in
742 a specified version (see \l {Type Revisions and Versions}).
743
744
745 For example, this registers a C++ class \c MySliderItem as a QML type
746 named \c Slider for version 1.0 of a type namespace called
747 "com.mycompany.qmlcomponents":
748
749 \code
750 qmlRegisterType<MySliderItem>("com.mycompany.qmlcomponents", 1, 0, "Slider");
751 \endcode
752
753 Once this is registered, the type can be used in QML by importing the
754 specified type namespace and version number:
755
756 \qml
757 import com.mycompany.qmlcomponents 1.0
758
759 Slider {
760 // ...
761 }
762 \endqml
763
764 Note that it's perfectly reasonable for a library to register types to older versions
765 than the actual version of the library. Indeed, it is normal for the new library to allow
766 QML written to previous versions to continue to work, even if more advanced versions of
767 some of its types are available.
768
769 \sa QML_ELEMENT, QML_NAMED_ELEMENT(),
770 {Choosing the Correct Integration Method Between C++ and QML}
771*/
772
773/*!
774 \fn template<typename T, int metaObjectRevision> int qmlRegisterRevision(const char *uri, int versionMajor, int versionMinor)
775 \relates QQmlEngine
776
777 This template function registers the specified revision of a C++ type in the QML system with
778 the library imported from \a uri having the version number composed
779 from \a versionMajor and \a versionMinor.
780
781 Returns the QML type id.
782
783 \code
784 template<typename T, int metaObjectRevision>
785 int qmlRegisterRevision(const char *uri, int versionMajor, int versionMinor);
786 \endcode
787
788 This function is typically used to register the revision of a base class to
789 use for the specified version of the type (see \l {Type Revisions and Versions}).
790*/
791
792/*!
793 \fn template <typename T> int qmlRegisterUncreatableType(const char *uri, int versionMajor, int versionMinor, const char *qmlName, const QString& message)
794 \relates QQmlEngine
795
796 This template function registers the C++ type in the QML system with
797 the name \a qmlName, in the library imported from \a uri having the
798 version number composed from \a versionMajor and \a versionMinor.
799
800 While the type has a name and a type, it cannot be created, and the
801 given error \a message will result if creation is attempted.
802
803 This is useful where the type is only intended for providing attached properties or enum values.
804
805 Returns the QML type id.
806
807 \sa QML_UNCREATABLE(), qmlRegisterTypeNotAvailable(),
808 {Choosing the Correct Integration Method Between C++ and QML}
809*/
810
811/*!
812 \fn template <typename T, typename E> int qmlRegisterExtendedType(const char *uri, int versionMajor, int versionMinor, const char *qmlName)
813 \relates QQmlEngine
814
815 This template function registers the C++ type and its extension object in the
816 QML system with the name \a qmlName in the library imported from \a uri having
817 version number composed from \a versionMajor and \a versionMinor. Properties
818 not available in the main type will be searched for in the extension object.
819
820 Returns the QML type id.
821
822 \sa QML_EXTENDED(), qmlRegisterType(), {Registering Extension Objects}
823*/
824
825
826/*!
827 \fn template <typename T, typename E> int qmlRegisterExtendedUncreatableType(const char *uri, int versionMajor, int versionMinor, const char *qmlName, const QString& reason)
828 \relates QQmlEngine
829
830 This template function registers the C++ type and its extension
831 in the QML system with the name \a qmlName in the library imported
832 from \a uri having version number composed from \a versionMajor and
833 \a versionMinor.
834
835 While the type has a name and a type, it cannot be created. An error
836 message with the given \a reason is printed if the user attempts to
837 create an instance of this type.
838
839 This is useful where the type is only intended for providing attached
840 properties, enum values or an abstract base class with its extension.
841
842 Returns the QML type id.
843
844 \sa QML_EXTENDED(), QML_UNCREATABLE(), qmlRegisterUncreatableType()
845*/
846
847/*!
848 \fn static inline int qmlRegisterUncreatableMetaObject(const QMetaObject &staticMetaObject, const char *uri, int versionMajor, int versionMinor, const char *qmlName, const QString& reason)
849 \relates QQmlEngine
850 \since 5.8
851
852 This function registers the \a staticMetaObject and its extension
853 in the QML system with the name \a qmlName in the library imported
854 from \a uri having version number composed from \a versionMajor and
855 \a versionMinor.
856
857 An instance of the meta object cannot be created. An error message with
858 the given \a reason is printed if the user attempts to create it.
859
860 This function is useful for registering Q_NAMESPACE namespaces.
861
862 Returns the QML type id.
863
864 For example:
865
866 \code
867 namespace MyNamespace {
868 Q_NAMESPACE
869 enum MyEnum {
870 Key1,
871 Key2,
872 };
873 Q_ENUM_NS(MyEnum)
874 }
875
876 //...
877 qmlRegisterUncreatableMetaObject(MyNamespace::staticMetaObject, "io.qt", 1, 0, "MyNamespace", "Access to enums & flags only");
878 \endcode
879
880 On the QML side, you can now use the registered enums:
881 \code
882 Component.onCompleted: console.log(MyNamespace.Key2)
883 \endcode
884
885 \sa QML_ELEMENT, QML_NAMED_ELEMENT(), QML_UNCREATABLE()
886*/
887
888/*!
889 \fn int qmlRegisterCustomExtendedType(const char *uri, int versionMajor, int versionMinor, const char *qmlName, QQmlCustomParser *parser)
890 \relates QQmlEngine
891 \internal
892
893 This template function registers the C++ type and its extension
894 in the QML system with the name \a qmlName in the library imported
895 from \a uri having version number composed from \a versionMajor and
896 \a versionMinor. Properties from the C++ type or its extension that
897 cannot be resolved directly by the QML system will be resolved using
898 the \a parser provided.
899
900 Returns the QML type id.
901
902 \sa QML_ELEMENT, QML_NAMED_ELEMENT(), QML_EXTENDED()
903*/
904
905/*!
906 \fn int qmlRegisterTypeNotAvailable(const char *uri, int versionMajor, int versionMinor, const char *qmlName, const QString& message)
907 \relates QQmlEngine
908
909 This function registers a type in the QML system with the name \a qmlName, in the type namespace imported from \a uri having the
910 version number composed from \a versionMajor and \a versionMinor, but any attempt to instantiate the type
911 will produce the given error \a message.
912
913 Normally, the types exported by a plugin should be fixed. However, if a C++ type is not available, you should
914 at least "reserve" the QML type name, and give the user of the unavailable type a meaningful error message.
915
916 Returns the QML type id.
917
918 Example:
919
920 \code
921 #ifdef NO_GAMES_ALLOWED
922 qmlRegisterTypeNotAvailable("MinehuntCore", 0, 1, "Game", "Get back to work, slacker!");
923 #else
924 qmlRegisterType<MinehuntGame>("MinehuntCore", 0, 1, "Game");
925 #endif
926 \endcode
927
928 This will cause any QML which imports the "MinehuntCore" type namespace and attempts to use the type to produce an error message:
929 \code
930 fun.qml: Get back to work, slacker!
931 Game {
932 ^
933 \endcode
934
935 Without this, a generic "Game is not a type" message would be given.
936
937 \sa QML_UNAVAILABLE, qmlRegisterUncreatableType(),
938 {Choosing the Correct Integration Method Between C++ and QML}
939*/
940
941/*!
942 \fn template <typename T> int qmlRegisterAnonymousType(const char *uri, int versionMajor)
943 \relates QQmlEngine
944
945 This template function registers the C++ type in the QML system as an anonymous type. The
946 resulting QML type does not have a name. Therefore, instances of this type cannot be created from
947 the QML system. You can, however, access instances of the type when they are exposed as properties
948 of other types.
949
950 Use this function when the type will not be referenced by name, specifically for C++ types that
951 are used on the left-hand side of a property binding. To indicate to which module the type belongs
952 use \a uri and \a versionMajor.
953
954 For example, consider the following two classes:
955
956 \code
957 class Bar : public QObject
958 {
959 Q_OBJECT
960 Q_PROPERTY(QString baz READ baz WRITE setBaz NOTIFY bazChanged)
961
962 public:
963 Bar() {}
964
965 QString baz() const { return mBaz; }
966
967 void setBaz(const QString &baz)
968 {
969 if (baz == mBaz)
970 return;
971
972 mBaz = baz;
973 emit bazChanged();
974 }
975
976 signals:
977 void bazChanged();
978
979 private:
980 QString mBaz;
981 };
982
983 class Foo : public QObject
984 {
985 Q_OBJECT
986 Q_PROPERTY(Bar *bar READ bar CONSTANT FINAL)
987
988 public:
989 Foo() {}
990
991 Bar *bar() { return &mBar; }
992
993 private:
994 Bar mBar;
995 };
996 \endcode
997
998 In QML, we assign a string to the \c baz property of \c bar:
999
1000 \code
1001 Foo {
1002 bar.baz: "abc"
1003 Component.onCompleted: print(bar.baz)
1004 }
1005 \endcode
1006
1007 For the QML engine to know that the \c Bar type has a \c baz property,
1008 we have to make \c Bar known:
1009
1010 \code
1011 qmlRegisterType<Foo>("App", 1, 0, "Foo");
1012 qmlRegisterAnonymousType<Bar>("App", 1);
1013 \endcode
1014
1015 As the \c Foo type is instantiated in QML, it must be registered
1016 with the version of \l qmlRegisterType() that takes an element name.
1017
1018 Returns the QML type id.
1019
1020 \since 5.14
1021 \sa QML_ANONYMOUS, {Choosing the Correct Integration Method Between C++ and QML}
1022*/
1023
1024/*!
1025 \fn int qmlRegisterInterface(const char *typeName)
1026 \relates QQmlEngine
1027
1028 This template function registers the C++ type in the QML system
1029 under the name \a typeName.
1030
1031 Types registered as an interface with the engine should also
1032 declare themselves as an interface with the
1033 \l {The Meta-Object System}{meta object system}. For example:
1034
1035 \code
1036 struct FooInterface
1037 {
1038 public:
1039 virtual ~FooInterface();
1040 virtual void doSomething() = 0;
1041 };
1042
1043 Q_DECLARE_INTERFACE(FooInterface, "org.foo.FooInterface")
1044 \endcode
1045
1046 When registered with the QML engine in this way, they can be used as
1047 property types:
1048
1049 Q_PROPERTY(FooInterface *foo READ foo WRITE setFoo)
1050
1051 When you assign a \l QObject sub-class to this property, the QML engine does
1052 the interface cast to \c FooInterface* automatically.
1053
1054 Returns the QML type id.
1055
1056 \sa QML_INTERFACE
1057*/
1058
1059/*!
1060 \fn int qmlRegisterSingletonType(const char *uri, int versionMajor, int versionMinor, const char *typeName, std::function<QJSValue(QQmlEngine *, QJSEngine *)> callback)
1061 \relates QQmlEngine
1062
1063 This function may be used to register a singleton type provider \a callback in a particular \a uri
1064 and \a typeName with a version specified in \a versionMajor and \a versionMinor.
1065
1066 Installing a singleton type allows developers to provide arbitrary functionality
1067 (methods and properties) to a client without requiring individual instances of the type to
1068 be instantiated by the client.
1069
1070 A singleton type may be either a QObject or a QJSValue.
1071 This function should be used to register a singleton type provider function which returns a QJSValue as a singleton type.
1072
1073 \b{NOTE:} QJSValue singleton type properties will \b{not} trigger binding re-evaluation if changed.
1074
1075 Usage:
1076 \code
1077 // First, define the singleton type provider function (callback).
1078 static QJSValue example_qjsvalue_singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
1079 {
1080 Q_UNUSED(engine)
1081
1082 static int seedValue = 5;
1083 QJSValue example = scriptEngine->newObject();
1084 example.setProperty("someProperty", seedValue++);
1085 return example;
1086 }
1087
1088 // Second, register the singleton type provider with QML by calling this function in an initialization function.
1089 qmlRegisterSingletonType("Qt.example.qjsvalueApi", 1, 0, "MyApi", example_qjsvalue_singletontype_provider);
1090 \endcode
1091
1092 Alternatively, you can use a C++11 lambda:
1093
1094 \code
1095 qmlRegisterSingletonType("Qt.example.qjsvalueApi", 1, 0, "MyApi", [](QQmlEngine *engine, QJSEngine *scriptEngine) -> QJSValue {
1096 Q_UNUSED(engine)
1097
1098 static int seedValue = 5;
1099 QJSValue example = scriptEngine->newObject();
1100 example.setProperty("someProperty", seedValue++);
1101 return example;
1102 });
1103 \endcode
1104
1105 In order to use the registered singleton type in QML, you must import the singleton type.
1106 \qml
1107 import QtQuick 2.0
1108 import Qt.example.qjsvalueApi 1.0 as ExampleApi
1109 Item {
1110 id: root
1111 property int someValue: ExampleApi.MyApi.someProperty
1112 }
1113 \endqml
1114
1115 \sa QML_SINGLETON, {Choosing the Correct Integration Method Between C++ and QML}
1116*/
1117
1118/*!
1119 \fn template<typename T> QObject *qmlAttachedPropertiesObject(const QObject *attachee, bool create)
1120 \relates QQmlEngine
1121
1122 The form of this template function is:
1123
1124 \code
1125 template<typename T> QObject *qmlAttachedPropertiesObject(const QObject *attachee, bool create = true)
1126 \endcode
1127
1128 This returns the attached object instance that has been attached to the specified
1129 \a attachee by the attaching type \e T.
1130
1131 If \a create is true and type \e T is a valid attaching type, this creates and returns a new
1132 attached object instance.
1133
1134 Returns \nullptr if type \e T is not a valid attaching type, or if \a create is false and no
1135 attachment object instance has previously been created for \a attachee.
1136
1137 \sa QML_ATTACHED(), {Providing Attached Properties}
1138*/
1139
1140/*!
1141 \fn QObject *qmlExtendedObject(const QObject *base)
1142 \relates QQmlEngine
1143
1144 This function returns the extension object that belongs to \a base, if there is any.
1145 Otherwise it returns \c nullptr.
1146
1147 \sa QML_EXTENDED
1148*/
1149
1150/*!
1151 \fn template <typename T> int qmlRegisterSingletonType(const char *uri, int versionMajor, int versionMinor, const char *typeName, std::function<QObject*(QQmlEngine *, QJSEngine *)> callback)
1152 \relates QQmlEngine
1153
1154 This function may be used to register a singleton type provider \a callback in a particular \a uri
1155 and \a typeName with a version specified in \a versionMajor and \a versionMinor.
1156
1157 Installing a singleton type into a uri allows developers to provide arbitrary functionality
1158 (methods and properties) to clients without requiring individual instances ot the type to be
1159 instantiated by the client.
1160
1161 A singleton type may be either a QObject or a QJSValue.
1162 This function should be used to register a singleton type provider function which returns a QObject
1163 of the given type T as a singleton type.
1164
1165 A QObject singleton type may be referenced via the type name with which it was registered, and this
1166 typename may be used as the target in a \l Connections type or otherwise used as any other type id would.
1167 One exception to this is that a QObject singleton type property may not be aliased.
1168
1169 \b{NOTE:} A QObject singleton type instance returned from a singleton type provider is owned by
1170 the QML engine unless the object has explicit QQmlEngine::CppOwnership flag set.
1171
1172 Usage:
1173 \code
1174 // First, define your QObject which provides the functionality.
1175 class SingletonTypeExample : public QObject
1176 {
1177 Q_OBJECT
1178 Q_PROPERTY (int someProperty READ someProperty WRITE setSomeProperty NOTIFY somePropertyChanged)
1179
1180 public:
1181 SingletonTypeExample(QObject *parent = nullptr)
1182 : QObject(parent), m_someProperty(0)
1183 {
1184 }
1185
1186 ~SingletonTypeExample() {}
1187
1188 Q_INVOKABLE int doSomething() { setSomeProperty(5); return m_someProperty; }
1189
1190 int someProperty() const { return m_someProperty; }
1191 void setSomeProperty(int val) { m_someProperty = val; emit somePropertyChanged(val); }
1192
1193 signals:
1194 void somePropertyChanged(int newValue);
1195
1196 private:
1197 int m_someProperty;
1198 };
1199
1200 // Second, define the singleton type provider function (callback).
1201 static QObject *example_qobject_singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
1202 {
1203 Q_UNUSED(engine)
1204 Q_UNUSED(scriptEngine)
1205
1206 SingletonTypeExample *example = new SingletonTypeExample();
1207 return example;
1208 }
1209
1210 // Third, register the singleton type provider with QML by calling this function in an initialization function.
1211 qmlRegisterSingletonType<SingletonTypeExample>("Qt.example.qobjectSingleton", 1, 0, "MyApi", example_qobject_singletontype_provider);
1212 \endcode
1213
1214 Alternatively, you can use a C++11 lambda:
1215
1216 \code
1217 qmlRegisterSingletonType<SingletonTypeExample>("Qt.example.qobjectSingleton", 1, 0, "MyApi", [](QQmlEngine *engine, QJSEngine *scriptEngine) -> QObject * {
1218 Q_UNUSED(engine)
1219 Q_UNUSED(scriptEngine)
1220
1221 SingletonTypeExample *example = new SingletonTypeExample();
1222 return example;
1223 });
1224 \endcode
1225
1226 In order to use the registered singleton type in QML, you must import the singleton type.
1227 \qml
1228 import QtQuick 2.0
1229 import Qt.example.qobjectSingleton 1.0
1230 Item {
1231 id: root
1232 property int someValue: MyApi.someProperty
1233
1234 Component.onCompleted: {
1235 someValue = MyApi.doSomething()
1236 }
1237 }
1238 \endqml
1239
1240 \sa QML_SINGLETON, {Choosing the Correct Integration Method Between C++ and QML}
1241*/
1242
1243/*!
1244 \fn int qmlRegisterSingletonType(const QUrl &url, const char *uri, int versionMajor, int versionMinor, const char *qmlName)
1245 \relates QQmlEngine
1246
1247 This function may be used to register a singleton type with the name \a qmlName, in the library imported from \a uri having
1248 the version number composed from \a versionMajor and \a versionMinor. The type is defined by the QML file located at \a url.
1249 The url must be an absolute URL, i.e. url.isRelative() == false.
1250
1251 In addition the type's QML file must have pragma Singleton statement among its import statements.
1252
1253 A singleton type may be referenced via the type name with which it was registered, and this typename may be used as the
1254 target in a \l Connections type or otherwise used as any other type id would. One exception to this is that a singleton
1255 type property may not be aliased (because the singleton type name does not identify an object within the same component
1256 as any other item).
1257
1258 Usage:
1259 \qml
1260 // First, define your QML singleton type which provides the functionality.
1261 pragma Singleton
1262 import QtQuick 2.0
1263 Item {
1264 property int testProp1: 125
1265 }
1266 \endqml
1267
1268 \code
1269 // Second, register the QML singleton type by calling this function in an initialization function.
1270 qmlRegisterSingletonType(QUrl("file:///absolute/path/SingletonType.qml"), "Qt.example.qobjectSingleton", 1, 0, "RegisteredSingleton");
1271 \endcode
1272
1273 In order to use the registered singleton type in QML, you must import the singleton type.
1274 \qml
1275 import QtQuick 2.0
1276 import Qt.example.qobjectSingleton 1.0
1277 Item {
1278 id: root
1279 property int someValue: RegisteredSingleton.testProp1
1280 }
1281 \endqml
1282
1283 It is also possible to have QML singleton types registered without using the qmlRegisterSingletonType function.
1284 That can be done by adding a pragma Singleton statement among the imports of the type's QML file. In addition
1285 the type must be defined in a qmldir file with a singleton keyword and the qmldir must be imported by the QML
1286 files using the singleton.
1287
1288 \sa QML_SINGLETON
1289*/
1290
1291/*!
1292 \fn int qmlRegisterSingletonInstance(const char *uri, int versionMajor, int versionMinor, const char *typeName, QObject *cppObject)
1293 \relates QQmlEngine
1294 \since 5.14
1295
1296 This function is used to register a singleton object \a cppObject, with a
1297 particular \a uri and \a typeName. Its version is a combination of \a
1298 versionMajor and \a versionMinor.
1299
1300 Installing a singleton type into a URI allows you to provide arbitrary
1301 functionality (methods and properties) to QML code without requiring
1302 individual instances of the type to be instantiated by the client.
1303
1304 Use this function to register an object of the given type T as a singleton
1305 type.
1306
1307 A QObject singleton type may be referenced via the type name with which it
1308 was registered; in turn this type name may be used as the target in a \l
1309 Connections type, or like any other type ID. However, there's one
1310 exception: a QObject singleton type property can't be aliased because the
1311 singleton type name does not identify an object within the same component
1312 as any other item.
1313
1314 \note \a cppObject must outlive the QML engine in which it is used.
1315 Moreover, \cppObject must have the same thread affinity as the engine. If
1316 you want separate singleton instances for multiple engines, you need to use
1317 \l {qmlRegisterSingletonType}. See \l{Threads and QObjects} for more
1318 information about thread safety.
1319
1320 \b{NOTE:} qmlRegisterSingleton can only be used when all types of that module are registered procedurally.
1321
1322 Usage:
1323 \code
1324 // First, define your QObject which provides the functionality.
1325 class SingletonTypeExample : public QObject
1326 {
1327 Q_OBJECT
1328 Q_PROPERTY(int someProperty READ someProperty WRITE setSomeProperty NOTIFY somePropertyChanged)
1329
1330 public:
1331 explicit SingletonTypeExample(QObject* parent = nullptr) : QObject(parent) {}
1332
1333 Q_INVOKABLE int doSomething()
1334 {
1335 setSomeProperty(5);
1336 return m_someProperty;
1337 }
1338
1339 int someProperty() const { return m_someProperty; }
1340 void setSomeProperty(int val) {
1341 if (m_someProperty != val) {
1342 m_someProperty = val;
1343 emit somePropertyChanged(val);
1344 }
1345 }
1346
1347 signals:
1348 void somePropertyChanged(int newValue);
1349
1350 private:
1351 int m_someProperty = 0;
1352 };
1353 \endcode
1354
1355 \code
1356 // Second, create an instance of the object
1357
1358 // allocate example before the engine to ensure that it outlives it
1359 QScopedPointer<SingletonTypeExample> example(new SingletonTypeExample);
1360 QQmlEngine engine;
1361
1362 // Third, register the singleton type provider with QML by calling this
1363 // function in an initialization function.
1364 qmlRegisterSingletonInstance("Qt.example.qobjectSingleton", 1, 0, "MyApi", example.get());
1365 \endcode
1366
1367
1368 In order to use the registered singleton type in QML, you must import the
1369 URI with the corresponding version.
1370 \qml
1371 import QtQuick 2.0
1372 import Qt.example.qobjectSingleton 1.0
1373 Item {
1374 id: root
1375 property int someValue: MyApi.someProperty
1376
1377 Component.onCompleted: {
1378 console.log(MyApi.doSomething())
1379 }
1380 }
1381 \endqml
1382
1383 \sa QML_SINGLETON, qmlRegisterSingletonType
1384 */
1385
1386/*!
1387 \fn int qmlRegisterType(const QUrl &url, const char *uri, int versionMajor, int versionMinor, const char *qmlName);
1388 \relates QQmlEngine
1389
1390 This function registers a type in the QML system with the name \a qmlName, in the library imported from \a uri having the
1391 version number composed from \a versionMajor and \a versionMinor. The type is defined by the QML file located at \a url. The
1392 url must be an absolute URL, i.e. url.isRelative() == false.
1393
1394 Normally QML files can be loaded as types directly from other QML files, or using a qmldir file. This function allows
1395 registration of files to types from C++ code, such as when the type mapping needs to be procedurally determined at startup.
1396
1397 Returns -1 if the registration was not successful.
1398*/
1399
1400/*!
1401 \fn bool qmlProtectModule(const char* uri, int majVersion);
1402 \relates QQmlEngine
1403
1404 This function protects a module from further modification. This can be used
1405 to prevent other plugins from injecting types into your module. It can also
1406 be a performance improvement, as it allows the engine to skip checking for
1407 the possibility of new types or plugins when this import is reached.
1408
1409 Once qmlProtectModule has been called, a QML engine will not search for a new
1410 \c qmldir file to load the module anymore. It will re-use any \c qmldir files
1411 it has loaded before, though. Therefore, types present at this point continue
1412 to work. Mind that different QML engines may load different modules. The
1413 module protection, however, is global and affects all engines. The overhead
1414 of locating \c qmldir files and loading plugins may be noticeable with slow file
1415 systems. Therefore, protecting a module once you are sure you won't need to
1416 load it anymore can be a good optimization. Mind also that the module lock
1417 not only affects plugins but also any other qmldir directives, like \c import
1418 or \c prefer, as well as any composite types or scripts declared in a \c qmldir
1419 file.
1420
1421 In addition, after this function is called, any attempt to register C++ types
1422 into this uri, major version combination will lead to a runtime error.
1423
1424 Returns true if the module with \a uri as a \l{Identified Modules}
1425 {module identifier} and \a majVersion as a major version number was found
1426 and locked, otherwise returns false. The module must contain exported types
1427 in order to be found.
1428*/
1429
1430/*!
1431 \since 5.9
1432 \fn void qmlRegisterModule(const char* uri, int versionMajor, int versionMinor);
1433 \relates QQmlEngine
1434
1435 This function registers a module in a particular \a uri with a version specified
1436 in \a versionMajor and \a versionMinor.
1437
1438 This can be used to make a certain module version available, even if no types
1439 are registered for that version. This is particularly useful for keeping the
1440 versions of related modules in sync.
1441*/
1442
1443/*!
1444 \since 5.12
1445 \fn int qmlTypeId(const char* uri, int versionMajor, int versionMinor, const char *qmlName);
1446 \relates QQmlEngine
1447
1448 Returns the QML type id of a type that was registered with the
1449 name \a qmlName in a particular \a uri and a version specified in \a
1450 versionMajor and \a versionMinor.
1451
1452 This function returns the same value as the QML type registration functions
1453 such as qmlRegisterType() and qmlRegisterSingletonType().
1454
1455 If \a qmlName, \a uri and \a versionMajor match a registered type, but the
1456 specified minor version in \a versionMinor is higher, then the id of the type
1457 with the closest minor version is returned.
1458
1459 Returns -1 if no matching type was found or one of the given parameters
1460 was invalid.
1461
1462 \sa QML_ELEMENT, QML_NAMED_ELEMENT, QML_SINGLETON, qmlRegisterType(), qmlRegisterSingletonType()
1463*/
1464
1465/*!
1466 \macro QML_VALUE_TYPE(name)
1467 \relates QQmlEngine
1468
1469 Declares the enclosing type or namespace to be available in QML, using \a name
1470 as the name. The type has to be a value type and the name has to be lower case.
1471
1472 \code
1473 class MyValueType
1474 {
1475 Q_GADGET
1476 QML_VALUE_TYPE(myValueType)
1477
1478 // ...
1479 };
1480 \endcode
1481
1482 \sa {Choosing the Correct Integration Method Between C++ and QML}, QML_NAMED_ELEMENT
1483*/
1484
1485/*!
1486 \macro QML_CONSTRUCTIBLE_VALUE
1487 \since 6.5
1488 \relates QQmlEngine
1489
1490 Marks the surrounding value type as constructible. That is, any \l Q_INVOKABLE
1491 constructors of the type that take exactly one argument can be used when
1492 assigning a JavaScript value to a property of this type.
1493
1494 You can declare a constructible value type as follows:
1495
1496 \code
1497 class MyValueType
1498 {
1499 Q_GADGET
1500 QML_VALUE_TYPE(myValueType)
1501 QML_CONSTRUCTIBLE_VALUE
1502 public:
1503 Q_INVOKABLE MyValueType(double d);
1504
1505 // ...
1506 };
1507 \endcode
1508
1509 With the above type, the following QML code will produce a \c MyValueType
1510 value using the given constructor and assign it to the property.
1511
1512 \qml
1513 QtObject {
1514 property myValueType v: 5.4
1515 }
1516 \endqml
1517
1518 You can also construct lists of values this way:
1519
1520 \qml
1521 QtObject {
1522 property list<myValueType> v: [5.4, 4.5, 3.3]
1523 }
1524 \endqml
1525
1526 If you make value types \l{ValueTypeBehavior}{addressable}, you can
1527 use such a type in a \l{Type annotations and assertions}{type assertion}
1528 to explicitly construct it:
1529
1530 \qml
1531 pragma ValueTypeBehavior: Addressable
1532
1533 QtObject {
1534 function process(d: real) {
1535 let v = d as myValueType;
1536 // v is a myValueType now, not a number
1537 }
1538 }
1539 \endqml
1540
1541 \sa QML_VALUE_TYPE
1542*/
1543
1544/*!
1545 \macro QML_STRUCTURED_VALUE
1546 \since 6.5
1547 \relates QQmlEngine
1548
1549 Marks the surrounding value type as structured. Structured value types can
1550 and will preferably be constructed property-by-property from a JavaScript
1551 object. A structured value type, however is always \l QML_CONSTRUCTIBLE_VALUE,
1552 too. This means, you can still provide \l Q_INVOKABLE constructors in order to
1553 handle construction from primitive types.
1554
1555 You can declare a structured value type as follows:
1556
1557 \code
1558 class MyValueType
1559 {
1560 Q_GADGET
1561 QML_VALUE_TYPE(myValueType)
1562 QML_STRUCTURED_VALUE
1563 Q_PROPERTY(double d READ d WRITE setD)
1564 Q_PROPERTY(string e READ e WRITE setE)
1565
1566 // ...
1567 };
1568 \endcode
1569
1570 Then you can populate a property of this type as follows:
1571
1572 \qml
1573 QtObject {
1574 property myValueType v: ({d: 4.4, e: "a string"})
1575 }
1576 \endqml
1577
1578 The extra parentheses are necessary to disambiguate the JavaScript object
1579 from what might be interpreted as a JavaScript code block.
1580
1581 You can also construct lists of values this way:
1582
1583 \qml
1584 QtObject {
1585 property list<myValueType> v: [
1586 {d: 4.4, e: "a string"},
1587 {d: 7.1, e: "another string"}
1588 ]
1589 }
1590 \endqml
1591
1592 If you make value types \l{ValueTypeBehavior}{addressable}, you can
1593 use such a type in a \l{Type annotations and assertions}{type assertion}
1594 to explicitly construct it:
1595
1596 \qml
1597 pragma ValueTypeBehavior: Addressable
1598
1599 QtObject {
1600 function process(d: real) {
1601 let v = {d: d, e: objectName} as myValueType;
1602 // v is a myValueType now
1603 }
1604 }
1605 \endqml
1606
1607 \sa QML_VALUE_TYPE QML_CONSTRUCTIBLE_VALUE
1608*/