summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/kernel
diff options
context:
space:
mode:
authorSimon Hausmann <simon.hausmann@qt.io>2020-01-03 10:15:18 +0100
committerSimon Hausmann <simon.hausmann@qt.io>2020-03-19 13:08:56 +0100
commit1fcce5105388b41c24c60324f678afb2d05e687f (patch)
tree3364991ecdf96c8200dd1f5703d43e1ba322909d /tests/auto/corelib/kernel
parent3c2aa878dceae77144e548eb839c7c76893874eb (diff)
Enable generic property bindings to QProperty<T>
A generic binding allows implementing the binding function in a way that enables the QML engine to run binding scripts and convert the V4::Value into a QVariant and then assign the value to the property with the help of QMetaType::construct. Change-Id: Id4807be92eee7e3501908e6c5e4c861cfcb7772a Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tests/auto/corelib/kernel')
-rw-r--r--tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp50
1 files changed, 50 insertions, 0 deletions
diff --git a/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp b/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp
index 7f64072df6..942865f3a3 100644
--- a/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp
+++ b/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp
@@ -65,6 +65,8 @@ private slots:
void changePropertyFromWithinChangeHandlerThroughDependency();
void changePropertyFromWithinChangeHandler2();
void settingPropertyValueDoesRemoveBinding();
+ void genericPropertyBinding();
+ void genericPropertyBindingBool();
};
void tst_QProperty::functorBinding()
@@ -610,6 +612,54 @@ void tst_QProperty::settingPropertyValueDoesRemoveBinding()
QVERIFY(property.binding().isNull());
}
+void tst_QProperty::genericPropertyBinding()
+{
+ QProperty<int> property;
+
+ {
+ QUntypedPropertyBinding doubleBinding(QMetaType::fromType<double>(),
+ [](const QMetaType &, void *) -> bool {
+ Q_ASSERT(false);
+ return false;
+ }, QPropertyBindingSourceLocation());
+ QVERIFY(!property.setBinding(doubleBinding));
+ }
+
+ QUntypedPropertyBinding intBinding(QMetaType::fromType<int>(),
+ [](const QMetaType &metaType, void *dataPtr) -> bool {
+ Q_ASSERT(metaType.id() == qMetaTypeId<int>());
+
+ int *intPtr = reinterpret_cast<int*>(dataPtr);
+ if (*intPtr == 100)
+ return false;
+ *intPtr = 100;
+ return true;
+ }, QPropertyBindingSourceLocation());
+
+ QVERIFY(property.setBinding(intBinding));
+
+ QCOMPARE(property.value(), 100);
+}
+
+void tst_QProperty::genericPropertyBindingBool()
+{
+ QProperty<bool> property;
+
+ QVERIFY(!property.value());
+
+ QUntypedPropertyBinding boolBinding(QMetaType::fromType<bool>(),
+ [](const QMetaType &, void *dataPtr) -> bool {
+ auto boolPtr = reinterpret_cast<bool *>(dataPtr);
+ if (*boolPtr)
+ return false;
+ *boolPtr = true;
+ return true;
+ }, QPropertyBindingSourceLocation());
+ QVERIFY(property.setBinding(boolBinding));
+
+ QVERIFY(property.value());
+}
+
QTEST_MAIN(tst_QProperty);
#include "tst_qproperty.moc"