QBindable: Mark non-modifying methods as const
A few methods in QBindable which do not modify anything were not marked as const so far. This adds the missing const, and a test to verify that they work. As all methods are fully inline, this does not cause any binary compatibility issues. Fixes: QTBUG-89508 Change-Id: If06d33bc405232887b8c371c268840ba34dbadf6 Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
parent
865f3c2a11
commit
d5b79e876e
@ -577,7 +577,7 @@ public:
|
|||||||
bool isBindable() const { return iface && iface->getBinding; }
|
bool isBindable() const { return iface && iface->getBinding; }
|
||||||
bool isReadOnly() const { return !(iface && iface->setBinding && iface->setObserver); }
|
bool isReadOnly() const { return !(iface && iface->setBinding && iface->setObserver); }
|
||||||
|
|
||||||
QUntypedPropertyBinding makeBinding(const QPropertyBindingSourceLocation &location = QT_PROPERTY_DEFAULT_BINDING_LOCATION)
|
QUntypedPropertyBinding makeBinding(const QPropertyBindingSourceLocation &location = QT_PROPERTY_DEFAULT_BINDING_LOCATION) const
|
||||||
{
|
{
|
||||||
return iface ? iface->makeBinding(data, location) : QUntypedPropertyBinding();
|
return iface ? iface->makeBinding(data, location) : QUntypedPropertyBinding();
|
||||||
}
|
}
|
||||||
@ -597,14 +597,14 @@ public:
|
|||||||
return binding;
|
return binding;
|
||||||
}
|
}
|
||||||
|
|
||||||
void observe(QPropertyObserver *observer)
|
void observe(QPropertyObserver *observer) const
|
||||||
{
|
{
|
||||||
if (iface)
|
if (iface)
|
||||||
iface->setObserver(data, observer);
|
iface->setObserver(data, observer);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Functor>
|
template<typename Functor>
|
||||||
QPropertyChangeHandler<Functor> onValueChanged(Functor f)
|
QPropertyChangeHandler<Functor> onValueChanged(Functor f) const
|
||||||
{
|
{
|
||||||
QPropertyChangeHandler<Functor> handler(f);
|
QPropertyChangeHandler<Functor> handler(f);
|
||||||
observe(&handler);
|
observe(&handler);
|
||||||
@ -612,7 +612,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename Functor>
|
template<typename Functor>
|
||||||
QPropertyChangeHandler<Functor> subscribe(Functor f)
|
QPropertyChangeHandler<Functor> subscribe(Functor f) const
|
||||||
{
|
{
|
||||||
f();
|
f();
|
||||||
return onValueChanged(f);
|
return onValueChanged(f);
|
||||||
@ -658,7 +658,7 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QPropertyBinding<T> makeBinding(const QPropertyBindingSourceLocation &location = QT_PROPERTY_DEFAULT_BINDING_LOCATION)
|
QPropertyBinding<T> makeBinding(const QPropertyBindingSourceLocation &location = QT_PROPERTY_DEFAULT_BINDING_LOCATION) const
|
||||||
{
|
{
|
||||||
return static_cast<QPropertyBinding<T> &&>(QUntypedBindable::makeBinding(location));
|
return static_cast<QPropertyBinding<T> &&>(QUntypedBindable::makeBinding(location));
|
||||||
}
|
}
|
||||||
|
@ -80,6 +80,7 @@ private slots:
|
|||||||
void typeNoOperatorEqual();
|
void typeNoOperatorEqual();
|
||||||
void bindingValueReplacement();
|
void bindingValueReplacement();
|
||||||
void quntypedBindableApi();
|
void quntypedBindableApi();
|
||||||
|
void readonlyConstQBindable();
|
||||||
|
|
||||||
void testNewStuff();
|
void testNewStuff();
|
||||||
void qobjectObservers();
|
void qobjectObservers();
|
||||||
@ -1030,6 +1031,26 @@ void tst_QProperty::quntypedBindableApi()
|
|||||||
QVERIFY(propLess.takeBinding().isNull());
|
QVERIFY(propLess.takeBinding().isNull());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void tst_QProperty::readonlyConstQBindable()
|
||||||
|
{
|
||||||
|
QProperty<int> i {42};
|
||||||
|
const QBindable<int> bindableI(const_cast<const QProperty<int> *>(&i));
|
||||||
|
|
||||||
|
// check that read-only operations work with a const QBindable
|
||||||
|
QVERIFY(bindableI.isReadOnly());
|
||||||
|
QVERIFY(!bindableI.hasBinding());
|
||||||
|
// we can still create a binding to a read only bindable through the interface
|
||||||
|
QProperty<int> j;
|
||||||
|
j.setBinding(bindableI.makeBinding());
|
||||||
|
QCOMPARE(j.value(), bindableI.value());
|
||||||
|
int counter = 0;
|
||||||
|
auto observer = bindableI.subscribe([&](){++counter;});
|
||||||
|
QCOMPARE(counter, 1);
|
||||||
|
auto observer2 = bindableI.onValueChanged([&](){++counter;});
|
||||||
|
i = 0;
|
||||||
|
QCOMPARE(counter, 3);
|
||||||
|
}
|
||||||
|
|
||||||
class MyQObject : public QObject
|
class MyQObject : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
Loading…
Reference in New Issue
Block a user