diff options
author | Alan Alpert <aalpert@rim.com> | 2013-01-24 12:13:57 -0800 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-02-18 19:46:02 +0100 |
commit | 99ad368cf810723643ae76bba6a1adba3321b18f (patch) | |
tree | e87c708650931c8863a1affed20befd1578aff35 /src/qml | |
parent | d0491c87a9f0f2dadc9ef0e0a267b8c241c08b41 (diff) |
Add qmlRegisterType for Composite Types
This is equivalent functionality to registering a composite type in a
qmldir file, a type name in a versioned module is associated with a
given file. This function now allows that to be done easily from C++.
Change-Id: I1cf44b92c3ad7fee593f4f84773c35b56253e628
Reviewed-by: Shawn Rutledge <shawn.rutledge@digia.com>
Diffstat (limited to 'src/qml')
-rw-r--r-- | src/qml/doc/src/qmlfunctions.qdoc | 17 | ||||
-rw-r--r-- | src/qml/qml/qqml.h | 21 |
2 files changed, 38 insertions, 0 deletions
diff --git a/src/qml/doc/src/qmlfunctions.qdoc b/src/qml/doc/src/qmlfunctions.qdoc index da62ddab6a..81b8fd594f 100644 --- a/src/qml/doc/src/qmlfunctions.qdoc +++ b/src/qml/doc/src/qmlfunctions.qdoc @@ -380,3 +380,20 @@ \endcode */ + +/*! + \fn int qmlRegisterType(const QUrl &url, const char *uri, int versionMajor, int versionMinor, const char *qmlName); + \relates QQmlEngine + + This function registers a type in the QML system with the name \a qmlName, in the library imported from \a uri having the + version number composed from \a versionMajor and \a versionMinor. The type is defined by the QML file located at \a url. The + url must be an absolute URL, i.e. url.isRelative() == false. + + Normally QML files can be loaded as types directly from other QML files, or using a qmldir file. This function allows + registration of files to types from a C++ module, such as when the type mapping needs to be procedurally determined at startup. + + #include <QtQml> to use this function. + + Returns non-zero if the registration was sucessful. +*/ + diff --git a/src/qml/qml/qqml.h b/src/qml/qml/qqml.h index 877e6c16b7..7e6e0d1d36 100644 --- a/src/qml/qml/qqml.h +++ b/src/qml/qml/qqml.h @@ -49,6 +49,7 @@ #include <QtCore/qbytearray.h> #include <QtCore/qmetaobject.h> +#include <QtCore/qdebug.h> #define QML_VERSION 0x020000 #define QML_VERSION_STR "2.0" @@ -463,6 +464,26 @@ inline int qmlRegisterSingletonType(const char *uri, int versionMajor, int versi return QQmlPrivate::qmlregister(QQmlPrivate::SingletonRegistration, &api); } + +inline int qmlRegisterType(const QUrl &url, const char *uri, int versionMajor, int versionMinor, const char *qmlName) +{ + if (url.isRelative()) { + // User input check must go here, because QQmlPrivate::qmlregister is also used internally for composite types + qWarning() << "qmlRegisterType requires absolute URLs."; + return 0; + } + + QQmlPrivate::RegisterCompositeType type = { + url, + uri, + versionMajor, + versionMinor, + qmlName + }; + + return QQmlPrivate::qmlregister(QQmlPrivate::CompositeRegistration, &type); +} + QT_END_NAMESPACE QML_DECLARE_TYPE(QObject) |