/*!
\module QtQml
- \title Qt Declarative Module
+ \title Qt Qml Module
\ingroup modules
- \brief The Qt Declarative module provides a declarative framework
+ \brief The Qt Qml module provides a declarative framework
for building highly dynamic, custom user interfaces.
To include the definitions of the module's classes, use the
.pro file:
\code
- QT += declarative
+ QT += qml
\endcode
- For more information on the Qt Declarative module, see the
+ For more information on the Qt Qml module (including the visual
+ elements which are implemented on top of the Qt Qml module) see the
\l{Qt Quick} documentation.
*/
*/
/*!
+ \internal
\fn int qmlRegisterModuleApi(const char *uri, int versionMajor, int versionMinor, QObject *(*callback)(QQmlEngine *, QJSEngine *))
+ \deprecated
+
+ Any uses of a module API in a binding where that module API was registered with this
+ function instead of the template version will result in suboptimal binding generation.
+ */
+
+/*!
+ \fn template<typename T> int qmlRegisterModuleApi(const char *uri, int versionMajor, int versionMinor, QObject *(*callback)(QQmlEngine *, QJSEngine *))
\relates QQmlEngine
This function may be used to register a module API provider \a callback in a particular \a uri
A module API may be either a QObject or a QJSValue. Only one module API provider
may be registered into any given namespace (combination of \a uri, \a versionMajor and \a versionMinor).
- This function should be used to register a module API provider function which returns a QObject as a module API.
+ This function should be used to register a module API provider function which returns a QObject
+ of the given type T as a module API.
A QObject module API must be imported with a qualifier, and that qualifier may be used as
the target in a \l Connections element or otherwise used as any other element id would.
One exception to this is that a QObject module API property may not be aliased (because the
module API qualifier does not identify an object within the same component as any other item).
+ \b{NOTE:} A QObject module API instance returned from a module API provider is owned by the QML
+ engine. For this reason, the module API provider function should \b{not} be implemented as a
+ singleton factory.
+
Usage:
\code
// first, define your QObject which provides the functionality.
void registerTypes(const char *uri)
{
Q_ASSERT(QLatin1String(uri) == "QtQuick.LocalStorage");
- qmlRegisterModuleApi(uri, 2, 0, module_api_factory);
+ qmlRegisterModuleApi<QQuickLocalStorage>(uri, 2, 0, module_api_factory);
}
};
uri, versionMajor, versionMinor,
- callback, 0
+ callback, 0, 0
};
return QQmlPrivate::qmlregister(QQmlPrivate::ModuleApiRegistration, &api);
uri, versionMajor, versionMinor,
- 0, callback
+ 0, callback, 0 // unknown QObject instance type
+ };
+
+ return QQmlPrivate::qmlregister(QQmlPrivate::ModuleApiRegistration, &api);
+}
+
+template <typename T>
+inline int qmlRegisterModuleApi(const char *uri, int versionMajor, int versionMinor,
+ QObject *(*callback)(QQmlEngine *, QJSEngine *))
+{
+ QQmlPrivate::RegisterModuleApi api = {
+ 1,
+
+ uri, versionMajor, versionMinor,
+
+ 0, callback, &T::staticMetaObject
};
return QQmlPrivate::qmlregister(QQmlPrivate::ModuleApiRegistration, &api);
a = new QQmlMetaType::ModuleApiInstance;
a->scriptCallback = module.script;
a->qobjectCallback = module.qobject;
+ a->instanceMetaObject = module.instanceMetaObject;
moduleApiInstances.insert(module, a);
}
import.minor = api.versionMinor;
import.script = api.scriptApi;
import.qobject = api.qobjectApi;
+ import.instanceMetaObject = (api.qobjectApi && api.version >= 1) ? api.instanceMetaObject : 0; // BC with version 0.
+
+ if (import.qobject && !import.instanceMetaObject) // BC - check import.iMO rather than api.iMO.
+ qWarning() << "qmlRegisterModuleApi(): sub-optimal: use the templated version of this function instead!";
int index = data->moduleApiCount++;
struct ModuleApiInstance {
ModuleApiInstance()
- : scriptCallback(0), qobjectCallback(0), qobjectApi(0) {}
+ : scriptCallback(0), qobjectCallback(0), qobjectApi(0), instanceMetaObject(0) {}
QJSValue (*scriptCallback)(QQmlEngine *, QJSEngine *);
QObject *(*qobjectCallback)(QQmlEngine *, QJSEngine *);
- QJSValue scriptApi;
QObject *qobjectApi;
+ const QMetaObject *instanceMetaObject;
+ QJSValue scriptApi;
+
};
struct ModuleApi {
inline ModuleApi();
inline bool operator==(const ModuleApi &) const;
int major;
int minor;
- QJSValue (*script)(QQmlEngine *, QJSEngine *);
QObject *(*qobject)(QQmlEngine *, QJSEngine *);
+ const QMetaObject *instanceMetaObject;
+ QJSValue (*script)(QQmlEngine *, QJSEngine *);
};
static ModuleApi moduleApi(const QString &, int, int);
static QHash<QString, QList<ModuleApi> > moduleApis();
{
major = 0;
minor = 0;
- script = 0;
qobject = 0;
+ instanceMetaObject = 0;
+ script = 0;
}
bool QQmlMetaType::ModuleApi::operator==(const ModuleApi &other) const
QJSValue (*scriptApi)(QQmlEngine *, QJSEngine *);
QObject *(*qobjectApi)(QQmlEngine *, QJSEngine *);
+ const QMetaObject *instanceMetaObject;
};
enum RegistrationType {
if (r.isValid()) {
if (r.type) {
_expr.code = _block->ATTACH_TYPE(name, r.type, IR::Name::ScopeStorage, line, column);
- } /*else if (r.importNamespace) {
+ } else if (r.importNamespace) {
QQmlMetaType::ModuleApiInstance *moduleApi = m_expression->importCache->moduleApi(r.importNamespace);
- if (moduleApi) {
- if (moduleApi->qobjectCallback) {
- moduleApi->qobjectApi = moduleApi->qobjectCallback(QQmlEnginePrivate::get(m_engine), QQmlEnginePrivate::get(m_engine));
- moduleApi->qobjectCallback = 0;
- moduleApi->scriptCallback = 0;
- }
- if (moduleApi->qobjectApi)
- _expr.code = _block->MODULE_OBJECT(name, moduleApi->qobjectApi->metaObject(), IR::Name::MemberStorage, line, column);
+ if (moduleApi && moduleApi->instanceMetaObject) {
+ // Note: we don't need to check moduleApi->qobjectCallback here, since
+ // we did that check in registerModuleApi() in qqmlmetatype.cpp.
+ // We cannot create the QObject Module Api Instance here,
+ // as we might be running in a loader thread.
+ // Thus, V4 can only handle bindings which use Module APIs which
+ // were registered with the templated registration function.
+ _expr.code = _block->MODULE_OBJECT(name, moduleApi->instanceMetaObject, IR::Name::MemberStorage, line, column);
}
- }*/ //### we can't create the actual QObject here, as we may be running in a thread (can be reenabled once QTBUG-24894 is handled)
+ }
// We don't support anything else
} else {
bool found = false;
--- /dev/null
+import QtQuick 2.0
+
+import Qt.test.legacyModuleApi 1.0 as ModApi // was registered with non-templated function
+
+QtObject {
+ property int legacyModulePropertyTest: ModApi.qobjectTestProperty
+ property int legacyModuleMethodTest
+
+ Component.onCompleted: {
+ legacyModuleMethodTest = ModApi.qobjectTestMethod();
+ }
+}
+
qRegisterMetaType<MyQmlObject::MyType>("MyQmlObject::MyType");
- qmlRegisterModuleApi("Qt.test",1,0,script_api); // register (script) module API for an existing uri which contains elements
- qmlRegisterModuleApi("Qt.test",1,0,qobject_api); // register (qobject) for an existing uri for which another module API was previously regd. Should replace!
- qmlRegisterModuleApi("Qt.test.scriptApi",1,0,script_api); // register (script) module API for a uri which doesn't contain elements
- qmlRegisterModuleApi("Qt.test.scriptApi",2,0,readonly_script_api); // register (script) module API for a uri which doesn't contain elements - will be made read-only
- qmlRegisterModuleApi("Qt.test.qobjectApi",1,0,qobject_api); // register (qobject) module API for a uri which doesn't contain elements
- qmlRegisterModuleApi("Qt.test.qobjectApi",1,3,qobject_api); // register (qobject) module API for a uri which doesn't contain elements, minor version set
- qmlRegisterModuleApi("Qt.test.qobjectApi",2,0,qobject_api); // register (qobject) module API for a uri which doesn't contain elements, major version set
- qmlRegisterModuleApi("Qt.test.qobjectApiParented",1,0,qobject_api_engine_parent); // register (parented qobject) module API for a uri which doesn't contain elements
+ qmlRegisterModuleApi("Qt.test",1,0,script_api); // register (script) module API for an existing uri which contains elements
+ qmlRegisterModuleApi<testQObjectApi>("Qt.test",1,0,qobject_api); // register (qobject) for an existing uri for which another module API was previously regd. Should replace!
+ qmlRegisterModuleApi("Qt.test.scriptApi",1,0,script_api); // register (script) module API for a uri which doesn't contain elements
+ qmlRegisterModuleApi("Qt.test.scriptApi",2,0,readonly_script_api); // register (script) module API for a uri which doesn't contain elements - will be made read-only
+ qmlRegisterModuleApi<testQObjectApi>("Qt.test.qobjectApi",1,0,qobject_api); // register (qobject) module API for a uri which doesn't contain elements
+ qmlRegisterModuleApi<testQObjectApi>("Qt.test.qobjectApi",1,3,qobject_api); // register (qobject) module API for a uri which doesn't contain elements, minor version set
+ qmlRegisterModuleApi<testQObjectApi>("Qt.test.qobjectApi",2,0,qobject_api); // register (qobject) module API for a uri which doesn't contain elements, major version set
+ qmlRegisterModuleApi<testQObjectApi>("Qt.test.qobjectApiParented",1,0,qobject_api_engine_parent); // register (parented qobject) module API for a uri which doesn't contain elements
+ qmlRegisterModuleApi("Qt.test.legacyModuleApi", 1, 0, qobject_api); // this registration function doesn't provide type information.
qRegisterMetaType<MyQmlObject::MyEnum2>("MyEnum2");
qRegisterMetaType<Qt::MouseButtons>("Qt::MouseButtons");
<< QVariantList()
<< QStringList()
<< QVariantList();
+
+ QTest::newRow("legacy module api registration")
+ << testFileUrl("moduleapi/qobjectModuleApiLegacy.qml")
+ << QString()
+ << QStringList() // warning doesn't occur in the test, but in registerTypes()
+ << (QStringList() << "legacyModulePropertyTest" << "legacyModuleMethodTest")
+ << (QVariantList() << 20 << 2)
+ << QStringList()
+ << QVariantList()
+ << QStringList()
+ << QVariantList();
}
void tst_qqmlecmascript::moduleApi()
// QTBUG-20937
void tst_qquickconnection::moduleApiTarget()
{
- qmlRegisterModuleApi("MyTestModuleApi", 1, 0, module_api_factory);
+ qmlRegisterModuleApi<MyTestModuleApi>("MyTestModuleApi", 1, 0, module_api_factory);
QQmlComponent component(&engine, testFileUrl("moduleapi-target.qml"));
QObject *object = component.create();
QVERIFY(object != 0);
--- /dev/null
+import Qt.test 1.0 as ModApi
+import QtQuick 2.0
+
+Item {
+ property int testProp: ModApi.ip
+ property int testProp2: 2
+
+ function getRandom() {
+ testProp2 = ModApi.random();
+ // testProp should also have changed.
+ }
+}
void mathCeil();
void mathMax();
void mathMin();
+ void moduleApi();
private:
QQmlEngine engine;
delete o;
}
+class V4ModuleApi : public QObject
+{
+ Q_OBJECT
+ Q_PROPERTY(int ip READ ip WRITE setIp NOTIFY ipChanged FINAL)
+public:
+ V4ModuleApi() : m_ip(12) {}
+ ~V4ModuleApi() {}
+
+ Q_INVOKABLE int random() { static int prng = 3; prng++; m_ip++; emit ipChanged(); return prng; }
+
+ int ip() const { return m_ip; }
+ void setIp(int v) { m_ip = v; emit ipChanged(); }
+
+signals:
+ void ipChanged();
+
+private:
+ int m_ip;
+};
+
+static QObject *v4_module_api_factory(QQmlEngine*, QJSEngine*)
+{
+ return new V4ModuleApi;
+}
+
+void tst_v4::moduleApi()
+{
+ // register module api, providing typeinfo via template
+ qmlRegisterModuleApi<V4ModuleApi>("Qt.test", 1, 0, v4_module_api_factory);
+ QQmlComponent component(&engine, testFileUrl("moduleApi.qml"));
+ QObject *o = component.create();
+ QVERIFY(o != 0);
+ QCOMPARE(o->property("testProp").toInt(), 12);
+ QCOMPARE(o->property("testProp2").toInt(), 2);
+ QMetaObject::invokeMethod(o, "getRandom");
+ QCOMPARE(o->property("testProp").toInt(), 13);
+ QCOMPARE(o->property("testProp2").toInt(), 4);
+ delete o;
+}
+
QTEST_MAIN(tst_v4)
#include "tst_v4.moc"