QBindable: Disallow mutation if read-only

If a QBindable is created from a computed property, it is not possible
to actually set a value or a binding. If we try to do it anyway, we'd
get a crash. Thus we now check whether the function pointer is null
before invoking it.

Pick-to: 6.0
Task-number: QTBUG-87153
Change-Id: I5bedb9080ccf79d9b8166b80d5733d095ed76f8d
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
Fabian Kosmale 2020-12-04 09:11:50 +01:00
parent e1440dd7bc
commit e236faa75f
2 changed files with 7 additions and 2 deletions

View File

@ -630,7 +630,7 @@ public:
QPropertyBinding<T> setBinding(const QPropertyBinding<T> &binding)
{
Q_ASSERT(!iface || binding.isNull() || binding.valueMetaType() == iface->metaType());
return iface ? static_cast<QPropertyBinding<T> &&>(iface->setBinding(data, binding)) : QPropertyBinding<T>();
return (iface && iface->setBinding) ? static_cast<QPropertyBinding<T> &&>(iface->setBinding(data, binding)) : QPropertyBinding<T>();
}
#ifndef Q_CLANG_QDOC
template <typename Functor>
@ -657,7 +657,7 @@ public:
void setValue(const T &value)
{
if (iface)
if (iface && iface->setter)
iface->setter(data, &value);
}
};

View File

@ -1114,6 +1114,11 @@ void tst_QProperty::testNewStuff()
object.readData.setValue(111);
QCOMPARE(object.computed(), 111);
object.bindableComputed().setBinding(object.bindableBar().makeBinding());
QCOMPARE(object.computed(), 111);
object.bindableComputed().setValue(10);
QCOMPARE(object.computed(), 111);
QCOMPARE(object.bindableFoo().value(), 111);
object.bindableFoo().setValue(24);
QCOMPARE(object.foo(), 24);