diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2021-10-19 15:22:25 +0200 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2021-10-20 15:51:23 +0200 |
commit | bf115a446c1a633a11577306eff8a78028a7f5b5 (patch) | |
tree | 8a2cd7650fe9d5ace7bdfdaa31669fccd8f1021a /tests/auto/qml/qqmlcomponent | |
parent | 974e2c668ae6e942f9e5d3eda8d77b1893af371f (diff) |
Improve type conversions from/to QJSValue
We can convert everything into a QJSValue if we have an engine and we
can save a binding function in a QVariant by wrapping it into QJSValue.
Change-Id: I48e7c13f3f744f1c50bf673b427fe9331250f313
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tests/auto/qml/qqmlcomponent')
-rw-r--r-- | tests/auto/qml/qqmlcomponent/tst_qqmlcomponent.cpp | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/tests/auto/qml/qqmlcomponent/tst_qqmlcomponent.cpp b/tests/auto/qml/qqmlcomponent/tst_qqmlcomponent.cpp index a92da98d2b..64b2207201 100644 --- a/tests/auto/qml/qqmlcomponent/tst_qqmlcomponent.cpp +++ b/tests/auto/qml/qqmlcomponent/tst_qqmlcomponent.cpp @@ -46,6 +46,28 @@ #include <algorithm> +class WithQJSValue : public QObject +{ + Q_OBJECT + Q_PROPERTY(QJSValue v READ v WRITE setV NOTIFY vChanged) + +public: + QJSValue v() const { return m_v; } + void setV(const QJSValue &newV) + { + if (!m_v.strictlyEquals(newV)) { + m_v = newV; + emit vChanged(); + } + } + +signals: + void vChanged(); + +private: + QJSValue m_v; +}; + class MyIC : public QObject, public QQmlIncubationController { Q_OBJECT @@ -130,6 +152,7 @@ private slots: void testSetInitialProperties(); void createInsideJSModule(); void qmlErrorIsReported(); + void initJSValueProp(); private: QQmlEngine engine; @@ -1024,6 +1047,22 @@ void tst_qqmlcomponent::qmlErrorIsReported() })); } +void tst_qqmlcomponent::initJSValueProp() +{ + qmlRegisterType<WithQJSValue>("ComponentTest", 1, 0, "WithQJSValue"); + QQmlEngine engine; + QQmlComponent component(&engine); + component.setData("import ComponentTest\nWithQJSValue {}", QUrl()); + QVERIFY2(component.isReady(), qPrintable(component.errorString())); + QScopedPointer<QObject> o(component.createWithInitialProperties({{ u"v"_qs, 5}})); + QVERIFY(!o.isNull()); + WithQJSValue *withQJSValue = qobject_cast<WithQJSValue *>(o.data()); + QVERIFY(withQJSValue); + const QJSValue jsValue = withQJSValue->v(); + QVERIFY(jsValue.isNumber()); + QCOMPARE(jsValue.toInt(), 5); +} + QTEST_MAIN(tst_qqmlcomponent) #include "tst_qqmlcomponent.moc" |