\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.
+*/
+
#include <QtCore/qbytearray.h>
#include <QtCore/qmetaobject.h>
+#include <QtCore/qdebug.h>
#define QML_VERSION 0x020000
#define QML_VERSION_STR "2.0"
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)
--- /dev/null
+3:1:Type RegisteredCompositeType2 unavailable
+-1:-1:File not found
--- /dev/null
+import Test 1.0
+
+RegisteredCompositeType2 {}
--- /dev/null
+3:1:Type RegisteredCompositeType3 unavailable
+1:1:Expected a qualified name id
--- /dev/null
+import Test 1.0
+
+RegisteredCompositeType3 {}
--- /dev/null
+import Test 1.0
+
+RegisteredCompositeType {}
void registrationOrder();
void readonly();
void receivers();
+ void registeredCompositeType();
void basicRemote_data();
void basicRemote();
QTest::newRow("invalidTypeName.4") << "invalidTypeName.4.qml" << "invalidTypeName.4.errors.txt" << false;
QTest::newRow("Major version isolation") << "majorVersionIsolation.qml" << "majorVersionIsolation.errors.txt" << false;
+
+ QTest::newRow("badCompositeRegistration.1") << "badCompositeRegistration.1.qml" << "badCompositeRegistration.1.errors.txt" << false;
+ QTest::newRow("badCompositeRegistration.2") << "badCompositeRegistration.2.qml" << "badCompositeRegistration.2.errors.txt" << false;
}
QQmlMetaType::registerCustomStringConverter(qMetaTypeId<MyCustomVariantType>(), myCustomVariantTypeConverter);
registerTypes();
+ // Registered here because it uses testFileUrl
+ qmlRegisterType(testFileUrl("CompositeType.qml"), "Test", 1, 0, "RegisteredCompositeType");
+ qmlRegisterType(testFileUrl("CompositeType.DoesNotExist.qml"), "Test", 1, 0, "RegisteredCompositeType2");
+ qmlRegisterType(testFileUrl("invalidRoot.1.qml"), "Test", 1, 0, "RegisteredCompositeType3");
// Registering the TestType class in other modules should have no adverse effects
qmlRegisterType<TestType>("org.qtproject.TestPre", 1, 0, "Test");
delete o;
}
+void tst_qqmllanguage::registeredCompositeType()
+{
+ QQmlComponent component(&engine, testFileUrl("registeredCompositeType.qml"));
+
+ VERIFY_ERRORS(0);
+ QObject *o = component.create();
+ QVERIFY(o != 0);
+
+ delete o;
+}
+
// QTBUG-18268
void tst_qqmllanguage::remoteLoadCrash()
{
qmlRegisterType<ValueInterceptorTestType>("Test", 1, 0, "ValueInterceptorTestType");
QUrl testTypeUrl(testFileUrl("CompositeType.qml"));
- //TODO: Replace this with public API version when added
- QQmlPrivate::RegisterCompositeType regStruct = {
- testTypeUrl,"Test", 1, 0, "TestTypeComposite"
- };
- QQmlPrivate::qmlregister(QQmlPrivate::CompositeRegistration, ®Struct);
+ qmlRegisterType(testTypeUrl, "Test", 1, 0, "TestTypeComposite");
}
void tst_qqmlmetatype::qmlParserStatusCast()