Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Constant expression gets changed twice
Qt 6.11 is out! See what's new in the release blog

Constant expression gets changed twice

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
8 Posts 2 Posters 2.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • DuBuD Offline
    DuBuD Offline
    DuBu
    wrote on last edited by
    #1

    Hello All,
    I noticed a strange behavior with a constant expression. Here's an example:

    import QtQuick 2.8
    import QtQuick.Window 2.2
    
    Window {
      visible: true
      width: 640
      height: 480
    
      property var a: []
    
      onAChanged: console.log("a:", a)
    
      Item {
        property var b: a
      }
    }
    

    When you run this code it outputs the following:

    qml: a: []
    qml: a: []
    

    Can someone explain, why "a" gets changed twice?

    If I remove the expression :

      property var b: a
    

    "a" gets only changed once as expected.

    1 Reply Last reply
    0
    • DuBuD Offline
      DuBuD Offline
      DuBu
      wrote on last edited by DuBu
      #2

      It get's even worse:

      import QtQuick 2.8
      import QtQuick.Window 2.2
      
      Window {
        visible: true
        width: 640
        height: 480
      
        property var a: []
        property var b: a
        property var c: b
        onAChanged: console.log("a:", a)
        onBChanged: console.log("b:", b)
        onCChanged: console.log("c:", c)
      
        Item {
          property var d: c
        }
      }
      

      Now the output is:

      qml: a: []
      qml: b: []
      qml: c: []
      qml: a: []
      qml: b: []
      qml: c: []
      qml: b: []
      qml: c: []
      qml: c: []
      

      "c" gets updated 4 times! Why?

      1 Reply Last reply
      0
      • DuBuD Offline
        DuBuD Offline
        DuBu
        wrote on last edited by
        #3

        No one? :-(

        E 1 Reply Last reply
        0
        • DuBuD DuBu

          No one? :-(

          E Offline
          E Offline
          Eeli K
          wrote on last edited by
          #4

          @DuBu Probably it's just how QML properties work. It's not recommended to have deep binding dependency chains anyways, and simple values (like integers or strings, not arrays or objects) seem to work more logically. If you have a real problem you have to do something about it, otherwise I think it's best to just let it be :)

          DuBuD 1 Reply Last reply
          0
          • E Eeli K

            @DuBu Probably it's just how QML properties work. It's not recommended to have deep binding dependency chains anyways, and simple values (like integers or strings, not arrays or objects) seem to work more logically. If you have a real problem you have to do something about it, otherwise I think it's best to just let it be :)

            DuBuD Offline
            DuBuD Offline
            DuBu
            wrote on last edited by
            #5

            @Eeli-K Thanks for your reply. Do you have any reference with some background informations about avoiding deep binding dependency chains?

            E 1 Reply Last reply
            0
            • DuBuD DuBu

              @Eeli-K Thanks for your reply. Do you have any reference with some background informations about avoiding deep binding dependency chains?

              E Offline
              E Offline
              Eeli K
              wrote on last edited by
              #6

              @DuBu Something like that was just mentioned in the Qt docs. I think there's nothing special in it. If you have chains like a: 'x'; b: a; c: b; d: c etc. it makes the program hard to understand, error prone and difficult to debug, and as you demonstrated, possibly slower. I think usually such deep chains aren't needed. If you really need them, use them. It's about common sense.

              DuBuD 1 Reply Last reply
              0
              • E Eeli K

                @DuBu Something like that was just mentioned in the Qt docs. I think there's nothing special in it. If you have chains like a: 'x'; b: a; c: b; d: c etc. it makes the program hard to understand, error prone and difficult to debug, and as you demonstrated, possibly slower. I think usually such deep chains aren't needed. If you really need them, use them. It's about common sense.

                DuBuD Offline
                DuBuD Offline
                DuBu
                wrote on last edited by
                #7

                @Eeli-K said in Constant expression gets changed twice:

                @DuBu Something like that was just mentioned in the Qt docs. I think there's nothing special in it. If you have chains like a: 'x'; b: a; c: b; d: c etc. it makes the program hard to understand, error prone and difficult to debug, and as you demonstrated, possibly slower.

                I agree. But my example is just a simplification. Here's a perhaps more practical snippet:

                  property var a: []
                  onAChanged: console.log("a:", a)
                
                  Item {
                    property var b: a
                    onBChanged: console.log("b:", b)
                
                    Item {
                      property var c: parent.b
                      onCChanged: console.log("c:", c)
                
                      Item {
                        property var d: parent.c
                        onDChanged: console.log("d:", d)
                      }
                    }
                  }
                

                Think of the 3 items as standalone components.
                The output is:

                qml: a: []
                qml: b: []
                qml: c: []
                qml: d: []
                qml: c: []
                qml: d: []
                qml: b: []
                qml: c: []
                qml: d: []
                qml: a: []
                qml: b: []
                qml: c: []
                qml: d: []
                

                I think usually such deep chains aren't needed. If you really need them, use them. It's about common sense.

                For me it's not common sense that after a correct initialization (first 4 output lines) the properties are updated up to 3 times more.
                In my understanding that's what bindings are for, to know what and when to update.

                E 1 Reply Last reply
                0
                • DuBuD DuBu

                  @Eeli-K said in Constant expression gets changed twice:

                  @DuBu Something like that was just mentioned in the Qt docs. I think there's nothing special in it. If you have chains like a: 'x'; b: a; c: b; d: c etc. it makes the program hard to understand, error prone and difficult to debug, and as you demonstrated, possibly slower.

                  I agree. But my example is just a simplification. Here's a perhaps more practical snippet:

                    property var a: []
                    onAChanged: console.log("a:", a)
                  
                    Item {
                      property var b: a
                      onBChanged: console.log("b:", b)
                  
                      Item {
                        property var c: parent.b
                        onCChanged: console.log("c:", c)
                  
                        Item {
                          property var d: parent.c
                          onDChanged: console.log("d:", d)
                        }
                      }
                    }
                  

                  Think of the 3 items as standalone components.
                  The output is:

                  qml: a: []
                  qml: b: []
                  qml: c: []
                  qml: d: []
                  qml: c: []
                  qml: d: []
                  qml: b: []
                  qml: c: []
                  qml: d: []
                  qml: a: []
                  qml: b: []
                  qml: c: []
                  qml: d: []
                  

                  I think usually such deep chains aren't needed. If you really need them, use them. It's about common sense.

                  For me it's not common sense that after a correct initialization (first 4 output lines) the properties are updated up to 3 times more.
                  In my understanding that's what bindings are for, to know what and when to update.

                  E Offline
                  E Offline
                  Eeli K
                  wrote on last edited by
                  #8

                  @DuBu You have to ask for example in the Qt Interest mailing list if you want to know why this actually happens, I don't know. I can just say that QML is declarative; it's meant to free you from thinking what happens inside. Therefore the inner workings are not necessarily intuitive. The problem comes from the fact that purely declarative programming isn't practical for our purposes and in QML/javascript declarative and imperative are mixed. Therefore you can accidentally bump into intricacies of declarative engine when you use something imperative. In practice what your snippet does may be like debugging a Prolog program by accident. In my opinion QML and this QML/javascript mix is far from perfect and will be superceded some day (within years or decades) but right now we have to live with strange things like this.

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved