Details
-
Bug
-
Resolution: Unresolved
-
P2: Important
-
6.5.2
-
None
Description
QObjectComputedProperty does not seem invoke observers unless we explicitly call notify() on it. Example:
#include <QtCore/QtDebug> #include <QtCore/QObject> #include <QtCore/QObjectBindableProperty> #include <QtCore/QObjectComputedProperty> class TestClass : public QObject { Q_OBJECT public: int getComputed() const { return foo * 2; } Q_OBJECT_BINDABLE_PROPERTY(TestClass, int, foo) Q_OBJECT_COMPUTED_PROPERTY(TestClass, int, computedData, &TestClass::getComputed) }; int main(int argc, char *argv[]) { TestClass test; auto notifier = test.computedData.addNotifier([&]() { qDebug() << "NOTIFY: computed=" << *test.computedData; }); test.foo = 2; // Uncomment to have the NOTIFY debug message printed // test.computedData.notify(); // The below works, so dependency tracking works fine. Just the observers // of QObjectComputerProperty don't. QProperty<int> other; other.setBinding([&] { return *test.computedData; }); auto otherNotifier = other.addNotifier([&] { qDebug() << "NOTIFY: other=" << *other; }); test.foo = 42; } #include "main.moc"
This is counter-intuitive. The docs explicitly state:
(Highlighting mine)To correctly participate in dependency handling, QObjectComputedProperty has to know when its value, the result of the callback given to it, might have changed. Whenever a bindable property used in the callback changes, this happens automatically.
Granted, it does only talk about dependency handling (and the last bit of code demonstrates that this works), but I would not have expected that observers needs special treatment.
In real-life, this makes implementing read-only properties unergonomic.