Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-118472

QObjectComputedProperty does not invoke observers

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P2: Important
    • 6.7
    • 6.5.2
    • Core: Other
    • 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:

      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.

      (Highlighting mine)

      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.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            fabiankosmale Fabian Kosmale
            msarehn Arno Rehn
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes