1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQml module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QQML_H
41#define QQML_H
42
43#include <QtQml/qqmlprivate.h>
44#include <QtQml/qqmlparserstatus.h>
45#include <QtQml/qqmlpropertyvaluesource.h>
46#include <QtQml/qqmllist.h>
47
48#include <QtCore/qbytearray.h>
49#include <QtCore/qmetaobject.h>
50
51#define QML_VERSION 0x020000
52#define QML_VERSION_STR "2.0"
53
54#define QML_DECLARE_TYPE(TYPE) \
55 Q_DECLARE_METATYPE(TYPE *) \
56 Q_DECLARE_METATYPE(QQmlListProperty<TYPE>)
57
58#define QML_DECLARE_TYPE_HASMETATYPE(TYPE) \
59 Q_DECLARE_METATYPE(QQmlListProperty<TYPE>)
60
61#define QML_DECLARE_INTERFACE(INTERFACE) \
62 QML_DECLARE_TYPE(INTERFACE)
63
64#define QML_DECLARE_INTERFACE_HASMETATYPE(INTERFACE) \
65 QML_DECLARE_TYPE_HASMETATYPE(INTERFACE)
66
67enum { /* TYPEINFO flags */
68 QML_HAS_ATTACHED_PROPERTIES = 0x01
69};
70
71#define QML_DECLARE_TYPEINFO(TYPE, FLAGS) \
72QT_BEGIN_NAMESPACE \
73template <> \
74class QQmlTypeInfo<TYPE > \
75{ \
76public: \
77 enum { \
78 hasAttachedProperties = (((FLAGS) & QML_HAS_ATTACHED_PROPERTIES) == QML_HAS_ATTACHED_PROPERTIES) \
79 }; \
80}; \
81QT_END_NAMESPACE
82
83QT_BEGIN_NAMESPACE
84
85
86class QQmlPropertyValueInterceptor;
87
88#define QML_GETTYPENAMES \
89 const char *className = T::staticMetaObject.className(); \
90 const int nameLen = int(strlen(className)); \
91 QVarLengthArray<char,48> pointerName(nameLen+2); \
92 memcpy(pointerName.data(), className, size_t(nameLen)); \
93 pointerName[nameLen] = '*'; \
94 pointerName[nameLen+1] = '\0'; \
95 const int listLen = int(strlen("QQmlListProperty<")); \
96 QVarLengthArray<char,64> listName(listLen + nameLen + 2); \
97 memcpy(listName.data(), "QQmlListProperty<", size_t(listLen)); \
98 memcpy(listName.data()+listLen, className, size_t(nameLen)); \
99 listName[listLen+nameLen] = '>'; \
100 listName[listLen+nameLen+1] = '\0';
101
102void Q_QML_EXPORT qmlClearTypeRegistrations();
103
104template<typename T>
105int qmlRegisterType()
106{
107 QML_GETTYPENAMES
108
109 QQmlPrivate::RegisterType type = {
110 0,
111
112 qRegisterNormalizedMetaType<T *>(pointerName.constData()),
113 qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
114 0,
115 nullptr,
116 QString(),
117
118 nullptr, 0, 0, nullptr, &T::staticMetaObject,
119
120 QQmlPrivate::attachedPropertiesFunc<T>(),
121 QQmlPrivate::attachedPropertiesMetaObject<T>(),
122
123 QQmlPrivate::StaticCastSelector<T,QQmlParserStatus>::cast(),
124 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueSource>::cast(),
125 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueInterceptor>::cast(),
126
127 nullptr, nullptr,
128
129 nullptr,
130 0
131 };
132
133 return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
134}
135
136int Q_QML_EXPORT qmlRegisterTypeNotAvailable(const char *uri, int versionMajor, int versionMinor, const char *qmlName, const QString& message);
137
138template<typename T>
139int qmlRegisterUncreatableType(const char *uri, int versionMajor, int versionMinor, const char *qmlName, const QString& reason)
140{
141 QML_GETTYPENAMES
142
143 QQmlPrivate::RegisterType type = {
144 0,
145
146 qRegisterNormalizedMetaType<T *>(pointerName.constData()),
147 qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
148 0,
149 nullptr,
150 reason,
151
152 uri, versionMajor, versionMinor, qmlName, &T::staticMetaObject,
153
154 QQmlPrivate::attachedPropertiesFunc<T>(),
155 QQmlPrivate::attachedPropertiesMetaObject<T>(),
156
157 QQmlPrivate::StaticCastSelector<T,QQmlParserStatus>::cast(),
158 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueSource>::cast(),
159 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueInterceptor>::cast(),
160
161 nullptr, nullptr,
162
163 nullptr,
164 0
165 };
166
167 return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
168}
169
170template<typename T, int metaObjectRevision>
171int qmlRegisterUncreatableType(const char *uri, int versionMajor, int versionMinor, const char *qmlName, const QString& reason)
172{
173 QML_GETTYPENAMES
174
175 QQmlPrivate::RegisterType type = {
176 1,
177
178 qRegisterNormalizedMetaType<T *>(pointerName.constData()),
179 qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
180 0,
181 nullptr,
182 reason,
183
184 uri, versionMajor, versionMinor, qmlName, &T::staticMetaObject,
185
186 QQmlPrivate::attachedPropertiesFunc<T>(),
187 QQmlPrivate::attachedPropertiesMetaObject<T>(),
188
189 QQmlPrivate::StaticCastSelector<T,QQmlParserStatus>::cast(),
190 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueSource>::cast(),
191 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueInterceptor>::cast(),
192
193 nullptr, nullptr,
194
195 nullptr,
196 metaObjectRevision
197 };
198
199 return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
200}
201
202template<typename T, typename E>
203int qmlRegisterExtendedUncreatableType(const char *uri, int versionMajor, int versionMinor, const char *qmlName, const QString& reason)
204{
205 QML_GETTYPENAMES
206
207 QQmlAttachedPropertiesFunc attached = QQmlPrivate::attachedPropertiesFunc<E>();
208 const QMetaObject * attachedMetaObject = QQmlPrivate::attachedPropertiesMetaObject<E>();
209 if (!attached) {
210 attached = QQmlPrivate::attachedPropertiesFunc<T>();
211 attachedMetaObject = QQmlPrivate::attachedPropertiesMetaObject<T>();
212 }
213
214 QQmlPrivate::RegisterType type = {
215 0,
216
217 qRegisterNormalizedMetaType<T *>(pointerName.constData()),
218 qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
219 0,
220 nullptr,
221 reason,
222
223 uri, versionMajor, versionMinor, qmlName, &T::staticMetaObject,
224
225 attached,
226 attachedMetaObject,
227
228 QQmlPrivate::StaticCastSelector<T,QQmlParserStatus>::cast(),
229 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueSource>::cast(),
230 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueInterceptor>::cast(),
231
232 QQmlPrivate::createParent<E>, &E::staticMetaObject,
233
234 nullptr,
235 0
236 };
237
238 return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
239}
240
241template<typename T, typename E, int metaObjectRevision>
242int qmlRegisterExtendedUncreatableType(const char *uri, int versionMajor, int versionMinor, const char *qmlName, const QString& reason)
243{
244 QML_GETTYPENAMES
245
246 QQmlAttachedPropertiesFunc attached = QQmlPrivate::attachedPropertiesFunc<E>();
247 const QMetaObject * attachedMetaObject = QQmlPrivate::attachedPropertiesMetaObject<E>();
248 if (!attached) {
249 attached = QQmlPrivate::attachedPropertiesFunc<T>();
250 attachedMetaObject = QQmlPrivate::attachedPropertiesMetaObject<T>();
251 }
252
253 QQmlPrivate::RegisterType type = {
254 1,
255
256 qRegisterNormalizedMetaType<T *>(pointerName.constData()),
257 qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
258 0,
259 nullptr,
260 reason,
261
262 uri, versionMajor, versionMinor, qmlName, &T::staticMetaObject,
263
264 attached,
265 attachedMetaObject,
266
267 QQmlPrivate::StaticCastSelector<T,QQmlParserStatus>::cast(),
268 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueSource>::cast(),
269 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueInterceptor>::cast(),
270
271 QQmlPrivate::createParent<E>, &E::staticMetaObject,
272
273 nullptr,
274 metaObjectRevision
275 };
276
277 return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
278}
279
280Q_QML_EXPORT int qmlRegisterUncreatableMetaObject(const QMetaObject &staticMetaObject, const char *uri, int versionMajor, int versionMinor, const char *qmlName, const QString& reason);
281
282template<typename T>
283int qmlRegisterType(const char *uri, int versionMajor, int versionMinor, const char *qmlName)
284{
285 QML_GETTYPENAMES
286
287 QQmlPrivate::RegisterType type = {
288 0,
289
290 qRegisterNormalizedMetaType<T *>(pointerName.constData()),
291 qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
292 sizeof(T), QQmlPrivate::createInto<T>,
293 QString(),
294
295 uri, versionMajor, versionMinor, qmlName, &T::staticMetaObject,
296
297 QQmlPrivate::attachedPropertiesFunc<T>(),
298 QQmlPrivate::attachedPropertiesMetaObject<T>(),
299
300 QQmlPrivate::StaticCastSelector<T,QQmlParserStatus>::cast(),
301 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueSource>::cast(),
302 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueInterceptor>::cast(),
303
304 nullptr, nullptr,
305
306 nullptr,
307 0
308 };
309
310 return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
311}
312
313template<typename T, int metaObjectRevision>
314int qmlRegisterType(const char *uri, int versionMajor, int versionMinor, const char *qmlName)
315{
316 QML_GETTYPENAMES
317
318 QQmlPrivate::RegisterType type = {
319 1,
320
321 qRegisterNormalizedMetaType<T *>(pointerName.constData()),
322 qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
323 sizeof(T), QQmlPrivate::createInto<T>,
324 QString(),
325
326 uri, versionMajor, versionMinor, qmlName, &T::staticMetaObject,
327
328 QQmlPrivate::attachedPropertiesFunc<T>(),
329 QQmlPrivate::attachedPropertiesMetaObject<T>(),
330
331 QQmlPrivate::StaticCastSelector<T,QQmlParserStatus>::cast(),
332 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueSource>::cast(),
333 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueInterceptor>::cast(),
334
335 nullptr, nullptr,
336
337 nullptr,
338 metaObjectRevision
339 };
340
341 return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
342}
343
344template<typename T, int metaObjectRevision>
345int qmlRegisterRevision(const char *uri, int versionMajor, int versionMinor)
346{
347 QML_GETTYPENAMES
348
349 QQmlPrivate::RegisterType type = {
350 1,
351
352 qRegisterNormalizedMetaType<T *>(pointerName.constData()),
353 qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
354 sizeof(T), QQmlPrivate::createInto<T>,
355 QString(),
356
357 uri, versionMajor, versionMinor, nullptr, &T::staticMetaObject,
358
359 QQmlPrivate::attachedPropertiesFunc<T>(),
360 QQmlPrivate::attachedPropertiesMetaObject<T>(),
361
362 QQmlPrivate::StaticCastSelector<T,QQmlParserStatus>::cast(),
363 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueSource>::cast(),
364 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueInterceptor>::cast(),
365
366 nullptr, nullptr,
367
368 nullptr,
369 metaObjectRevision
370 };
371
372 return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
373}
374
375
376template<typename T, typename E>
377int qmlRegisterExtendedType()
378{
379 QML_GETTYPENAMES
380
381 QQmlPrivate::RegisterType type = {
382 0,
383
384 qRegisterNormalizedMetaType<T *>(pointerName.constData()),
385 qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
386 0,
387 nullptr,
388 QString(),
389
390 nullptr, 0, 0, nullptr, &T::staticMetaObject,
391
392 QQmlPrivate::attachedPropertiesFunc<T>(),
393 QQmlPrivate::attachedPropertiesMetaObject<T>(),
394
395 QQmlPrivate::StaticCastSelector<T,QQmlParserStatus>::cast(),
396 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueSource>::cast(),
397 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueInterceptor>::cast(),
398
399 QQmlPrivate::createParent<E>, &E::staticMetaObject,
400
401 nullptr,
402 0
403 };
404
405 return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
406}
407
408template<typename T, typename E>
409int qmlRegisterExtendedType(const char *uri, int versionMajor, int versionMinor,
410 const char *qmlName)
411{
412 QML_GETTYPENAMES
413
414 QQmlAttachedPropertiesFunc attached = QQmlPrivate::attachedPropertiesFunc<E>();
415 const QMetaObject * attachedMetaObject = QQmlPrivate::attachedPropertiesMetaObject<E>();
416 if (!attached) {
417 attached = QQmlPrivate::attachedPropertiesFunc<T>();
418 attachedMetaObject = QQmlPrivate::attachedPropertiesMetaObject<T>();
419 }
420
421 QQmlPrivate::RegisterType type = {
422 0,
423
424 qRegisterNormalizedMetaType<T *>(pointerName.constData()),
425 qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
426 sizeof(T), QQmlPrivate::createInto<T>,
427 QString(),
428
429 uri, versionMajor, versionMinor, qmlName, &T::staticMetaObject,
430
431 attached,
432 attachedMetaObject,
433
434 QQmlPrivate::StaticCastSelector<T,QQmlParserStatus>::cast(),
435 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueSource>::cast(),
436 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueInterceptor>::cast(),
437
438 QQmlPrivate::createParent<E>, &E::staticMetaObject,
439
440 nullptr,
441 0
442 };
443
444 return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
445}
446
447template<typename T>
448int qmlRegisterInterface(const char *typeName)
449{
450 QByteArray name(typeName);
451
452 QByteArray pointerName(name + '*');
453 QByteArray listName("QQmlListProperty<" + name + '>');
454
455 QQmlPrivate::RegisterInterface qmlInterface = {
456 0,
457
458 qRegisterNormalizedMetaType<T *>(pointerName.constData()),
459 qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
460
461 qobject_interface_iid<T *>()
462 };
463
464 return QQmlPrivate::qmlregister(QQmlPrivate::InterfaceRegistration, &qmlInterface);
465}
466
467template<typename T>
468int qmlRegisterCustomType(const char *uri, int versionMajor, int versionMinor,
469 const char *qmlName, QQmlCustomParser *parser)
470{
471 QML_GETTYPENAMES
472
473 QQmlPrivate::RegisterType type = {
474 0,
475
476 qRegisterNormalizedMetaType<T *>(pointerName.constData()),
477 qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
478 sizeof(T), QQmlPrivate::createInto<T>,
479 QString(),
480
481 uri, versionMajor, versionMinor, qmlName, &T::staticMetaObject,
482
483 QQmlPrivate::attachedPropertiesFunc<T>(),
484 QQmlPrivate::attachedPropertiesMetaObject<T>(),
485
486 QQmlPrivate::StaticCastSelector<T,QQmlParserStatus>::cast(),
487 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueSource>::cast(),
488 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueInterceptor>::cast(),
489
490 nullptr, nullptr,
491
492 parser,
493 0
494 };
495
496 return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
497}
498
499template<typename T, int metaObjectRevision>
500int qmlRegisterCustomType(const char *uri, int versionMajor, int versionMinor,
501 const char *qmlName, QQmlCustomParser *parser)
502{
503 QML_GETTYPENAMES
504
505 QQmlPrivate::RegisterType type = {
506 1,
507
508 qRegisterNormalizedMetaType<T *>(pointerName.constData()),
509 qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
510 sizeof(T), QQmlPrivate::createInto<T>,
511 QString(),
512
513 uri, versionMajor, versionMinor, qmlName, &T::staticMetaObject,
514
515 QQmlPrivate::attachedPropertiesFunc<T>(),
516 QQmlPrivate::attachedPropertiesMetaObject<T>(),
517
518 QQmlPrivate::StaticCastSelector<T,QQmlParserStatus>::cast(),
519 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueSource>::cast(),
520 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueInterceptor>::cast(),
521
522 nullptr, nullptr,
523
524 parser,
525 metaObjectRevision
526 };
527
528 return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
529}
530
531template<typename T, typename E>
532int qmlRegisterCustomExtendedType(const char *uri, int versionMajor, int versionMinor,
533 const char *qmlName, QQmlCustomParser *parser)
534{
535 QML_GETTYPENAMES
536
537 QQmlAttachedPropertiesFunc attached = QQmlPrivate::attachedPropertiesFunc<E>();
538 const QMetaObject * attachedMetaObject = QQmlPrivate::attachedPropertiesMetaObject<E>();
539 if (!attached) {
540 attached = QQmlPrivate::attachedPropertiesFunc<T>();
541 attachedMetaObject = QQmlPrivate::attachedPropertiesMetaObject<T>();
542 }
543
544 QQmlPrivate::RegisterType type = {
545 0,
546
547 qRegisterNormalizedMetaType<T *>(pointerName.constData()),
548 qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
549 sizeof(T), QQmlPrivate::createInto<T>,
550 QString(),
551
552 uri, versionMajor, versionMinor, qmlName, &T::staticMetaObject,
553
554 attached,
555 attachedMetaObject,
556
557 QQmlPrivate::StaticCastSelector<T,QQmlParserStatus>::cast(),
558 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueSource>::cast(),
559 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueInterceptor>::cast(),
560
561 QQmlPrivate::createParent<E>, &E::staticMetaObject,
562
563 parser,
564 0
565 };
566
567 return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
568}
569
570class QQmlContext;
571class QQmlEngine;
572class QJSValue;
573class QJSEngine;
574
575#ifndef Q_QDOC
576namespace QtQml {
577#endif
578 // declared in namespace to avoid symbol conflicts with QtDeclarative
579 Q_QML_EXPORT void qmlExecuteDeferred(QObject *);
580 Q_QML_EXPORT QQmlContext *qmlContext(const QObject *);
581 Q_QML_EXPORT QQmlEngine *qmlEngine(const QObject *);
582#if QT_DEPRECATED_SINCE(5, 14)
583 Q_QML_EXPORT QT_DEPRECATED QObject *qmlAttachedPropertiesObjectById(int, const QObject *, bool create = true);
584 Q_QML_EXPORT QT_DEPRECATED QObject *qmlAttachedPropertiesObject(
585 int *, const QObject *, const QMetaObject *, bool create);
586#endif
587 Q_QML_EXPORT QQmlAttachedPropertiesFunc qmlAttachedPropertiesFunction(QObject *,
588 const QMetaObject *);
589 Q_QML_EXPORT QObject *qmlAttachedPropertiesObject(QObject *, QQmlAttachedPropertiesFunc func,
590 bool create = true);
591#ifndef Q_QDOC
592}
593
594QT_WARNING_PUSH
595QT_WARNING_DISABLE_CLANG("-Wheader-hygiene")
596
597// This is necessary to allow for QtQuick1 and QtQuick2 scenes in a single application.
598using namespace QtQml;
599
600QT_WARNING_POP
601
602#endif // Q_QDOC
603
604//The C++ version of protected namespaces in qmldir
605Q_QML_EXPORT bool qmlProtectModule(const char* uri, int majVersion);
606Q_QML_EXPORT void qmlRegisterModule(const char *uri, int versionMajor, int versionMinor);
607
608template<typename T>
609QObject *qmlAttachedPropertiesObject(const QObject *obj, bool create = true)
610{
611 // We don't need a concrete object to resolve the function. As T is a C++ type, it and all its
612 // super types should be registered as CppType (or not at all). We only need the object and its
613 // QML engine to resolve composite types. Therefore, the function is actually a static property
614 // of the C++ type system and we can cache it here for improved performance on further lookups.
615 static const auto func = qmlAttachedPropertiesFunction(nullptr, &T::staticMetaObject);
616 return qmlAttachedPropertiesObject(const_cast<QObject *>(obj), func, create);
617}
618
619inline int qmlRegisterSingletonType(const char *uri, int versionMajor, int versionMinor, const char *typeName,
620 QJSValue (*callback)(QQmlEngine *, QJSEngine *))
621{
622 QQmlPrivate::RegisterSingletonType api = {
623 0,
624
625 uri, versionMajor, versionMinor, typeName,
626
627 callback, nullptr, nullptr, 0, 0, {}
628 };
629
630 return QQmlPrivate::qmlregister(QQmlPrivate::SingletonRegistration, &api);
631}
632
633enum { QmlCurrentSingletonTypeRegistrationVersion = 3 };
634template <typename T>
635inline int qmlRegisterSingletonType(const char *uri, int versionMajor, int versionMinor, const char *typeName,
636 QObject *(*callback)(QQmlEngine *, QJSEngine *))
637{
638 QML_GETTYPENAMES
639
640 QQmlPrivate::RegisterSingletonType api = {
641 QmlCurrentSingletonTypeRegistrationVersion,
642
643 uri, versionMajor, versionMinor, typeName,
644
645 nullptr, nullptr, &T::staticMetaObject, qRegisterNormalizedMetaType<T *>(pointerName.constData()), 0, callback
646 };
647
648 return QQmlPrivate::qmlregister(QQmlPrivate::SingletonRegistration, &api);
649}
650
651template <typename T, typename F, typename std::enable_if<std::is_convertible<F, std::function<QObject *(QQmlEngine *, QJSEngine *)>>::value
652 && !std::is_convertible<F, QObject *(*)(QQmlEngine *, QJSEngine *)>::value, void>::type* = nullptr>
653inline int qmlRegisterSingletonType(const char *uri, int versionMajor, int versionMinor, const char *typeName,
654 F&& callback)
655{
656
657 QML_GETTYPENAMES
658
659 QQmlPrivate::RegisterSingletonType api = {
660 QmlCurrentSingletonTypeRegistrationVersion,
661
662 uri, versionMajor, versionMinor, typeName,
663
664 nullptr, nullptr, &T::staticMetaObject, qRegisterNormalizedMetaType<T *>(pointerName.constData()), 0, callback
665 };
666
667 return QQmlPrivate::qmlregister(QQmlPrivate::SingletonRegistration, &api);
668}
669
670template<typename T>
671inline auto qmlRegisterSingletonInstance(const char *uri, int versionMajor, int versionMinor,
672 const char *typeName, T *cppObject) -> typename std::enable_if<std::is_base_of<QObject, T>::value, int>::type
673{
674 QQmlPrivate::RegisterSingletonFunctor registrationFunctor;
675 registrationFunctor.m_object = cppObject;
676 return qmlRegisterSingletonType<T>(uri, versionMajor, versionMinor, typeName, registrationFunctor);
677}
678
679inline int qmlRegisterSingletonType(const QUrl &url, const char *uri, int versionMajor, int versionMinor, const char *qmlName)
680{
681 if (url.isRelative()) {
682 // User input check must go here, because QQmlPrivate::qmlregister is also used internally for composite types
683 qWarning("qmlRegisterSingletonType requires absolute URLs.");
684 return 0;
685 }
686
687 QQmlPrivate::RegisterCompositeSingletonType type = {
688 url,
689 uri,
690 versionMajor,
691 versionMinor,
692 qmlName
693 };
694
695 return QQmlPrivate::qmlregister(QQmlPrivate::CompositeSingletonRegistration, &type);
696}
697
698inline int qmlRegisterType(const QUrl &url, const char *uri, int versionMajor, int versionMinor, const char *qmlName)
699{
700 if (url.isRelative()) {
701 // User input check must go here, because QQmlPrivate::qmlregister is also used internally for composite types
702 qWarning("qmlRegisterType requires absolute URLs.");
703 return 0;
704 }
705
706 QQmlPrivate::RegisterCompositeType type = {
707 url,
708 uri,
709 versionMajor,
710 versionMinor,
711 qmlName
712 };
713
714 return QQmlPrivate::qmlregister(QQmlPrivate::CompositeRegistration, &type);
715}
716
717int Q_QML_EXPORT qmlTypeId(const char *uri, int versionMajor, int versionMinor, const char *qmlName);
718
719QT_END_NAMESPACE
720
721QML_DECLARE_TYPE(QObject)
722Q_DECLARE_METATYPE(QVariant)
723
724#endif // QQML_H
725