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

QProperty onChangeValued() calls the callback to many times

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P2: Important
    • None
    • 6.0.0 Beta2
    • Core: Object Model
    • None
    • All

    Description

      I think that onValueChanged() should only be called one time in the following code.

      #include <QProperty>
      
      int main(int argc, char *argv[])
      {
          QProperty<int> x;
          QProperty<int> y;
          QProperty<int> z(10);
          QProperty<QString> str;
      
          int xCount = 0;
          x.setBinding([&]() {
              xCount++;
              return y.value() * 10;
          });
      
          int yCount = 0;
          y.setBinding([&]() {
              yCount++;
              return z.value() * 5;
          });
      
          int strCount = 0;
          str.setBinding([&](){
              strCount++;
              return QString("x:%1 y:%2 z:%3").arg(x.value()).arg(y.value()).arg(z.value());
          });
      
          int valueChangeCount = 0;
          auto valueChangedHandle = str.onValueChanged([&valueChangeCount, &str](){
              valueChangeCount++;
              qDebug() << "Called:" << str.value();
          });
      
          z.setValue(13);
      
          //Change valueChangeCount should only be 1, instead of 3?
          qDebug() << "Str:" << valueChangeCount << str.value() << valueChangeCount;
      }
      

       

      Instead the change handler gets call 3 times because:

      x depends on y

      y depends on z

      str depends on x, y, z

      When updating z, it cause y binding to execute, then x binding, which I guess is 3 changes, but since this cascade happen from a single set from z, it might be more efficient if onValueChanged() is only executed once.

      Attachments

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

        Activity

          People

            laknoll Lars Knoll
            pschuchardt Philip Schuchardt
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes