diff options
Diffstat (limited to 'src/qml/jsapi')
-rw-r--r-- | src/qml/jsapi/qjsengine.cpp | 43 | ||||
-rw-r--r-- | src/qml/jsapi/qjsengine.h | 8 | ||||
-rw-r--r-- | src/qml/jsapi/qjsvalue.cpp | 36 | ||||
-rw-r--r-- | src/qml/jsapi/qjsvalue.h | 2 |
4 files changed, 89 insertions, 0 deletions
diff --git a/src/qml/jsapi/qjsengine.cpp b/src/qml/jsapi/qjsengine.cpp index 09e8bbda55..4404a5d79f 100644 --- a/src/qml/jsapi/qjsengine.cpp +++ b/src/qml/jsapi/qjsengine.cpp @@ -166,6 +166,17 @@ Q_DECLARE_METATYPE(QList<int>) properties of the proxy object. No binding code is needed because it is done dynamically using the Qt meta object system. + Use newQMetaObject() to wrap a QMetaObject; this gives you a + "script representation" of a QObject-based class. newQMetaObject() + returns a proxy script object; enum values of the class are available + as properties of the proxy object. + + Constructors exposed to the meta-object system ( using Q_INVOKABLE ) can be + called from the script to create a new QObject instance with + JavaScriptOwnership. + + + \snippet code/src_script_qjsengine.cpp 5 \section1 Extensions @@ -511,6 +522,38 @@ QJSValue QJSEngine::newQObject(QObject *object) } /*! + \since 5.8 + + Creates a JavaScript object that wraps the given QMetaObject + The metaObject must outlive the script engine. It is recommended to only + use this method with static metaobjects. + + + When called as a constructor, a new instance of the class will be created. + Only constructors exposed by Q_INVOKABLE will be visible from the script engine. + + \sa newQObject() +*/ + +QJSValue QJSEngine::newQMetaObject(const QMetaObject* metaObject) { + Q_D(QJSEngine); + QV4::ExecutionEngine *v4 = QV8Engine::getV4(d); + QV4::Scope scope(v4); + QV4::ScopedValue v(scope, QV4::QMetaObjectWrapper::create(v4, metaObject)); + return QJSValue(v4, v->asReturnedValue()); +} + +/*! \fn QJSValue QJSEngine::newQMetaObject<T>() + + \since 5.8 + Creates a JavaScript object that wraps the static QMetaObject associated + with class \c{T}. + + \sa newQObject() +*/ + + +/*! Returns this engine's Global Object. By default, the Global Object contains the built-in objects that are diff --git a/src/qml/jsapi/qjsengine.h b/src/qml/jsapi/qjsengine.h index 6ecd0c7ec0..41c4b81270 100644 --- a/src/qml/jsapi/qjsengine.h +++ b/src/qml/jsapi/qjsengine.h @@ -74,6 +74,14 @@ public: QJSValue newQObject(QObject *object); + QJSValue newQMetaObject(const QMetaObject* metaObject); + + template <typename T> + QJSValue newQMetaObject() + { + return newQMetaObject(&T::staticMetaObject); + } + template <typename T> inline QJSValue toScriptValue(const T &value) { diff --git a/src/qml/jsapi/qjsvalue.cpp b/src/qml/jsapi/qjsvalue.cpp index 4860908bd3..44746b8c2b 100644 --- a/src/qml/jsapi/qjsvalue.cpp +++ b/src/qml/jsapi/qjsvalue.cpp @@ -1234,6 +1234,28 @@ QObject *QJSValue::toQObject() const } /*! + \since 5.8 + + * If this QJSValue is a QMetaObject, returns the QMetaObject pointer + * that the QJSValue represents; otherwise, returns 0. + * + * \sa isQMetaObject() + */ +const QMetaObject *QJSValue::toQMetaObject() const +{ + QV4::ExecutionEngine *engine = QJSValuePrivate::engine(this); + if (!engine) + return 0; + QV4::Scope scope(engine); + QV4::Scoped<QV4::QMetaObjectWrapper> wrapper(scope, QJSValuePrivate::getValue(this)); + if (!wrapper) + return 0; + + return wrapper->metaObject(); +} + + +/*! Returns a QDateTime representation of this value, in local time. If this QJSValue is not a date, or the value of the date is NaN (Not-a-Number), an invalid QDateTime is returned. @@ -1286,4 +1308,18 @@ bool QJSValue::isQObject() const return val && val->as<QV4::QObjectWrapper>() != 0; } +/*! + \since 5.8 + + Returns true if this QJSValue is a QMetaObject; otherwise returns + false. + + \sa toQMetaObject(), QJSEngine::newQMetaObject() +*/ +bool QJSValue::isQMetaObject() const +{ + QV4::Value *val = QJSValuePrivate::getValue(this); + return val && val->as<QV4::QMetaObjectWrapper>() != 0; +} + QT_END_NAMESPACE diff --git a/src/qml/jsapi/qjsvalue.h b/src/qml/jsapi/qjsvalue.h index e207e1b099..ab20a2607d 100644 --- a/src/qml/jsapi/qjsvalue.h +++ b/src/qml/jsapi/qjsvalue.h @@ -98,6 +98,7 @@ public: bool isUndefined() const; bool isVariant() const; bool isQObject() const; + bool isQMetaObject() const; bool isObject() const; bool isDate() const; bool isRegExp() const; @@ -111,6 +112,7 @@ public: bool toBool() const; QVariant toVariant() const; QObject *toQObject() const; + const QMetaObject *toQMetaObject() const; QDateTime toDateTime() const; bool equals(const QJSValue &other) const; |